MongoDB - Environment

MongoDB - Environment

Let's now see how to install MongoDB on Windows.

Install MongoDB on Windows

To install MongoDB on Windows, first download the latest version of MongoDB from . Make sure you are getting the correct version of MongoDB based on your version of Windows. To get the Windows version, open a command prompt and run the following command.

C:\>wmic os get osarchitecture

The 32-bit versions of MongoDB only support databases smaller than 2GB and are suitable for testing and evaluation purposes only.

Now extract the downloaded file to c:\ drive or any other location. Make sure the name of the extracted folder is mongodb-win32-i386-[version] or mongodb-win32-x86_64-[version]. Where [version] is the download version of MongoDB.

Then open a command prompt and run the following command.

C:\>move mongodb-win64-* mongodb
   1 dir(s) moved.

If you checked out MongoDB from somewhere else, navigate to that path with the cd FOOLDER /DIR command and now start the above process.

MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is c:\data\db. Thus, you need to create this folder using the command line. Run the following sequence of commands.


If you need to install MongoDB elsewhere, you need to provide an alternate path for \data\db by specifying the dbpath in mongod.exe For the same, run the following commands.

At the command prompt, change to the bin directory located in the MongoDB installation folder. Suppose my installation folder is D:\set up\mongodb

D:\>cd "set up"
D:\setup>cd mongodb
D:\setup\mongodb>cd bin
D:\set up\mongodb\bin>mongod.exe --dbpath "d:\set up\mongodb\data"

This will display a message about pending connections in the console output indicating that the mongod.exe process is running successfully.

Now, to start MongoDB, you need to open another command prompt and run the following command.

MongoDB shell version: 2.4.6
connecting to: test
> { a: 1 } )
{ "_id" : ObjectId(5879b0f65a56a454), "a" : 1 }

This will show that MongoDB is installed and started successfully. The next time you start MongoDB, you only need to enter commands.

D:\set up\mongodb\bin>mongod.exe --dbpath "d:\set up\mongodb\data"

Install MongoDB on Ubuntu

Run the following command to import GPG MongoDB public key −

sudo apt-key adv --keyserver hkp:// --recv 7F0CEB10

Create the /etc/apt/sources.list.d/mongodb.list file with the following command.

echo 'deb dist 10gen'
   | sudo tee /etc/apt/sources.list.d/mongodb.list

Now run the following command to update the repository −

sudo apt-get update

Then install MongoDB using the following command −

apt-get install mongodb-10gen=2.2.3

The above 2.2.3 installation has the MongoDB version currently released. Always install the latest version. Now MongoDB has been successfully installed.

Start MongoDB

sudo service mongodb start

Stop MongoDB

sudo service mongodb stop

Restart MongoDB

sudo service mongodb restart

To use MongoDB, run the following command.


This will connect you to a running MongoDB instance.

MongoDB Help

To get a list of commands, type in the MongoDB client. This will give you a list of commands as shown in the following screenshot.

Database Help

MongoDB Statistics

To get statistics about the MongoDB server, issue the db.stats() command in the MongoDB client. This will show the database name, collection number, and documents in the database. The output of the command is shown in the following screenshot.