CHAPTER 2
MongoDB is a cross-platform database, and the current version supports a variety of operating systems—pretty much all main Linux distributions, Microsoft Windows, and OSX.
As the idea of this book was to use Microsoft.NET and MongoDB, we will only briefly touch on Linux installation as a reference, and we will mainly concentrate on running and configuring MongoDB on Microsoft Windows. While all concepts apply equally on every platform, it’s not in the scope of this book to get into particular platform details.
For Mac and Windows platforms, you can download MongoDB and install it directly from the
project website.
In this book, we will be covering the MongoDB Community Server, which is a free version of MongoDB.
Examples in this book were tested in Microsoft Windows 10; the exact version downloaded and used is version 3.4.1 for “Windows Server 2008 R2 64-bit and later, with SSL support x64.”
MongoDB can run as a standalone application or as a Windows service. For learning purposes, it is interesting to see MongoDB running as the standalone application (console), as it outputs some debug information that can be useful. Otherwise, on production systems, it’s highly recommended to run it as a Windows service.
Starting the database is as simple as running the following command.
Code Listing 3: Starting MongoDB
C:\mongodb\bin>mongod |
After running this command (which stands for “mongo daemon”) for the first time, you might be asked to open the firewall. Accept this option.
The output will be similar to the following in Figure 7, from which we may learn quite a few things (as highlighted).

Figure 7: Starting MongoDB.
By running the following command, where we specify explicitly where to place the logs, we will install MongoDB as a Windows Service. Make sure to run the command prompt with administrator rights.
In order to install MongoDB on Windows as a service, keeping the previously used settings is as simple as adding the --install parameter and the location of the log specified by the value of --logpath.
Code Listing 4: Installing MongoDB as a Windows service
C:\mongodb\bin>mongod --logpath c:\data\log\log.log –-install |
If the installation is successful, you will see MongoDB in the Windows Services panel, as shown in Figure 8.
For more information about installing MongoDB on Windows with the full option list, see this tutorial.

Figure 8: MongoDB service after complete installation.
Here we will demonstrate how to install MongoDB on Ubuntu (version 16.04 at the time of writing) by using the popular apt package management tool.
Using the terminal window, let’s follow the following steps:
Code Listing 5: Importing the public key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927 |
Code Listing 6: List file creation
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list |
Code Listing 7: Reload local package database
sudo apt-get update |
At this step, after preparing the apt package manager, we can install the latest version of MongoDB by running:
Code Listing 8: Install MongoDB package
sudo apt-get install -y mongodb-org |
Or, alternatively, install a specific version of MongoDB (in our case, version 3.4.10).
Code Listing 9: Installing a specific version of MongoDB
sudo apt-get install -y mongodb-org=3.4.10 mongodb-org-server=3.4.10 mongodb-org-shell=3.4.10 mongodb-org-mongos=3.4.10 mongodb-org-tools=3.4.10 |
Code Listing 10: MongoDB configuration file content
[Unit] Description=High-performance, schema-free document-oriented database After=network.target Documentation=https://docs.mongodb.org/manual [Service] User=mongodb Group=mongodb ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf [Install] WantedBy=multi-user.target |
Code Listing 11: Starting the MongoDB
sudo service mongod start |
Let’s check to see if it’s possible to connect to the database:
Code Listing 12: Starting the client via terminal
mongo |
This command should successfully connect to the “test” database.
We have just seen how to install MongoDB, together with the database itself. There are also a few extra components that are part of the installation package. In this chapter, we are going to go through the list and explain briefly what each of them does.

Figure 9: MongoDB installed utilities.
Don’t worry if you’re not familiar with some of the concepts (such as GridFS) that we haven’t touched yet—we’ll be looking into those in the next chapters.
Table 2: MongoDB installation files
Component | Category | Description |
|---|---|---|
Core Process | The core database process. It is responsible for running (hosting) the mongodb instance on a single machine. This process is responsible for handling connections, requests, data access, and any other operations typical of a database. | |
Core Process | Short for “MongoDB Shard,” mongos is a service that sits between the application and the MongoDBs. It communicates with the configuration server to determine where the requested data lives (on which shard). It then fetches, aggregates, and returns the data in JSON form. | |
Core Process | An interactive JavaScript shell interface to MongoDB that provides an interface for system administrators and allows any kind of operation against the MongoDB instance. | |
Binary Import/Export | A utility for creating a binary export of the contents of a database. | |
Binary Import/Export | Writes data from a binary database dump created by mongodump to a MongoDB instance. | |
Binary Import/Export | bsondump is a diagnostic tool for inspecting BSON files, and it converts BSON files into human-readable formats, including JSON, which is very useful for reading the output files generated by mongodump. | |
Binary Import/Export | A simple tool that polls operations from the replication oplog of a remote server, and applies them to the local server. | |
Data Import/Export | Imports content from a JSON, CSV, or TSV export created by mongoexport. | |
Data Import/Export | Produces a JSON or CSV export of data stored in a MongoDB instance. | |
Diagnostics | Provides a quick overview of the status of a currently running mongod or mongos instance. It allows the monitoring of the MongoDB. It provides information such as the server status, database statistics, and collection statistics. | |
Diagnostics | Another monitoring tool that provides the method to track the amount of time a MongoDB instance spends reading and writing data. It provides statistics per collection. | |
Diagnostics | A traffic capture and replay tool for MongoDB that you can use to inspect and record commands sent to a MongoDB instance, and then replay those commands back onto another host at a later time. | |
Diagnostics | A utility that checks disk I/O performance independently of MongoDB. | |
GridFS | Manipulates files stored in the MongoDB instance in GridFSobjects from the command line. It is particularly useful, as it provides an interface between objects stored in your file system and GridFS. |