How to work with MongoDB: installation and configuration

Lecture



The progress of NoSQL technologies that are replacing long-established relational databases. Yesterday the web relied on a handful of well-known RDBMSs, yet today around five NoSQL solutions have already emerged and proven themselves.

Despite how abrupt such changes may look, it can actually take years for them to become common practice. Initial enthusiasm typically draws in only a small number of developers and companies. Solutions get refined, lessons get learned - and once it becomes clear that the new technology is maturing, everyone else starts trying to apply it to their own needs. Again, this applies to the NoSQL space, where many technologies are not so much a direct replacement for more traditional storage mechanisms as they are solutions to specific problems, supplementing what you'd expect from traditional systems.
Keeping all of this in mind, we need to understand what NoSQL actually is. It's a broad term that means different things to different people. Personally, I use it in a broad sense, to refer to a system involved in storing data. On the other hand, to me NoSQL also means the conviction that the task of storing data shouldn't be placed on one single large system. While the makers of most databases have historically tried to position their software as an "all in one" solution, NoSQL strives for a smaller scope of responsibility - where, for a given task, you can choose the tool that solves exactly that task in the best way. For example, your NoSQL stack might make effective use of relational databases, such as MySQL, but it might also include Redis - for key-value storage - or Hadoop - for intensive data processing. Simply put, NoSQL is an open technology made up of alternative, existing, and complementary data-management patterns.
Surprisingly, MongoDB fits all of these definitions. As a document-oriented DBMS, Mongo is a fairly general-purpose NoSQL solution. It can be viewed as an alternative to relational DBMSs. Like relational DBMSs, it can also be usefully supplemented by more specialized NoSQL solutions. MongoDB has both strengths and weaknesses, which we'll discuss in later parts of the book.
As you've probably noticed, the terms MongoDB and Mongo are used interchangeably.
The basics of MongoDB. For that we'll need the MongoDB console. The console will be used for learning and administrative tasks, while in code we'll use the MongoDB driver.
How to work with MongoDB: installation and configuration

We've arrived at the first thing you need to know about MongoDB: its drivers. MongoDB has a large number of official drivers for various languages. You can think of them as drivers for databases you're already used to. On top of them, the developer community has built a range of higher-level drivers for specific languages and frameworks. For example, NoRM is a library for C# that implements LINQ, and MongoMapper is for Ruby, with ActiveRecord support. Whether to program directly against MongoDB's low-level drivers, or to use higher-level libraries instead, is up to you. I've dwelt on this because many newcomers get confused by the existence of both official drivers and community-built ones - the former are aimed at basic communication with Mongo, while the latter focus more on integrating with specific languages and frameworks.

As you read, try to reproduce the examples shown, and also explore any questions that come up along the way. Getting MongoDB up and running is easy - it'll only take us a few minutes to set everything up.

  1. Go to the official download page and download the binaries from the first line (the recommended stable version) for the operating system you're using. For development you can use either the 32-bit or the 64-bit version.
  2. Unpack the archive (anywhere you like) and go into the bin folder. Don't run anything yet, but remember that mongod is the server, and mongo is the client console - these are the two executables we'll be working with most often.
  3. Create a new file in the bin folder and name it mongodb.config
  4. Add a single line to mongodb.config: dbpath=PATH_WHERE_YOU_WANT_TO_STORE_THE_DATABASE_FILES.

    For example, on Windows you could write dbpath=c:\mongodb\data and on Linux - dbpath=/etc/mongodb/data.

  5. Make sure the dbpath path you specified exists.
  6. Run mongod with the --config /path/to/your/mongodb.config parameter.

For Windows users, for example, if you unpacked the downloaded file into c:\mongodb\ and created the folder c:\mongodb\data\, then in c:\mongodb\bin\mongodb.config you should specify db?path=c:\mongodb\data\. Now you can run mongod from the command line using the command c:\mongodb\bin\mongod --config c:\mongodb\bin\mongodb.config.

For convenience, you can add the bin folder to the PATH environment variable. For MacOSX and Linux users, the instructions are practically the same. All you need to do is just change the paths.

I hope MongoDB is now installed and running for you. If there are errors, read the messages in the console carefully - the server prints detailed and clear diagnostic messages.

Now, to connect to the running server, you can start mongo (without the d at the end). Try entering db.version() to make sure everything is fine. If everything's OK, you'll see your server's version number.

When you start working with MongoDB in the mongo shell, the first thing you need to do is set the database you need as the current one, so you can then use it. To do this, use the use command, followed by the name of the database. It doesn't matter whether that database already exists or not. If it doesn't exist yet, MongoDB will automatically create it once you add data to it.

So, let's launch the mongo.exe shell and enter the following command there:

1
> use test

How to work with MongoDB: installation and configuration

Now the test database will be set as the current one.

If you're ever not sure whether a database with that name already exists, you can use the show dbs command to print the names of all existing databases to the console:

How to work with MongoDB: installation and configuration

You can give a database any name, but there are some restrictions. For example, the name must not contain the characters /, \, ., ", *, <, >, :, |, ?, $. In addition, database names are limited to 64 bytes.

There are also reserved names that you can't use: local, admin, config. These names represent databases that already exist by default on the server and are intended for internal purposes.

And as you can see, the test database isn't in this list, since I haven't added any data to it yet.

If we want to find out which database is currently in use, we can use the db command:

1
2
> db
test

Besides databases, we can also view the list of all collections in the current database using the show collections command.

Getting statistics

Using the db.stats() command, you can get statistics for the current database. For example, we currently have the test database set as the current one:

How to work with MongoDB: installation and configuration

In a similar way, we can find out all the statistics for a specific collection. For example, let's get the statistics for the users collection: db.users.stats()

How to work with MongoDB: installation and configuration

See also

  • [[b8218]]
  • [[b9900]]
  • [[b9901]]
  • [[b9902]]
  • [[b9903]]
  • [[b9904]]
  • [[b9905]]
  • [[b9906]]
  • [[b9907]]

See also

created: 2020-12-18
updated: 2026-03-10
116



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Databases -MongoDB"

Terms: Databases -MongoDB