Home >>MongoDB Tutorial >MongoDB Environment

MongoDB Environment

MongoDB Environment

Install MongoDB On Windows

To install MongoDB on Windows, first download the latest release of MongoDB from https://www.mongodb.com/download-center.

MongoDB Environment

Enter the required details, select the Server tab, choose the MongoDB version, the operating system and the package as follows:

Install the downloaded file now and instal it by default in folder C:\Program Files\.

To store its files, MongoDB includes a data folder. For the MongoDB data directory, the default location is c:\data\db. So this folder needs to be created using a Command Prompt. Execute the command sequence that follows.

C:\>md data
C:\md data\db

Then you need to set the dbpath to the directory you built in the mongod.exe directory. Issue the following commands to do the same.

Navigate to the bin directory present in the MongoDB installation folder in the Command Prompt. Suppose C:\Program Files\MongoDB is my installation folder,

C:\Users\XYZ>d:cd C:\Program Files\MongoDB\Server\4.2\bin
C:\Program Files\MongoDB\Server\4.2\bin>mongod.exe --dbpath "C:\data"

This will show a message on the console output waiting for connections, indicating that the mongod.exe process is running successfully.

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

C:\Program Files\MongoDB\Server\4.2\bin>mongo.exe

MongoDB shell version v4.2.1

connecting to:

mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("4260beda-f662-4cbe-9bc7-5c1f2242663c") }

MongoDB server version: 4.2.1

This will show that MongoDB has been successfully installed and running. Next time you run MongoDB, only commands need to be given.

C:\Program Files\MongoDB\Server\4.2\bin>mongod.exe --dbpath "C:\data"
C:\Program Files\MongoDB\Server\4.2\bin>mongo.exe

Install MongoDB on Ubuntu

Run the following command to import the MongoDB public GPG key –

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

Create a /etc/apt/sou

	echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' 
   | sudo tee /etc/apt/sources.list.d/mongodb.list
	

Now issue the following command to update the repository −

sudo apt-get update

Next install the MongoDB by using the following command −

apt-get install mongodb-10gen = 4.2

2.2.3 is currently released in the MongoDB version of the installation above. Always make sure you install the latest version. MongoDB has now 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 running MongoDB instance.

MongoDB Statistics

Type the db.stats command on the MongoDB client to get stats about the MongoDB server. The database name, the collection number and the documents in the database will be displayed.


No Sidebar ads