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 https://www.mongodb.org/downloads . 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 OSArchitecture 64-bit C:\>
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. C:\>
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.
C:\>mddata C:\mddata\db
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
C:\Users\XYZ>d: 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.
D:\setup\mongodb\bin>mongo.exe MongoDB shell version: 2.4.6 connecting to: test >db.test.save( { a: 1 } ) >db.test.find() { "_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" D:\setup\mongodb\bin>mongo.exe
Install MongoDB on Ubuntu
Run the following command to import GPG MongoDB public key −
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Create the /etc/apt/sources.list.d/mongodb.list file with the following command.
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart 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.
mongo
This will connect you to a running MongoDB instance.
MongoDB Help
To get a list of commands, type db.help() in the MongoDB client. This will give you a list of commands as shown in the following screenshot.
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.