MongoDB: How to get up and running in less than 5 minutes (on Windows)

MongoDB is a “NoSQL” (non-relational) data store of documents that have no predefined schema, where data is stored as a series of JSON objects. The concept of the relational database where there is a database that has tables which are made up of columns and rows, is replaced by the MongoDB concept of a database consisting of “collections”, which are made up of documents full of JSON objects.

This article will focus mainly on the installation of MongoDB on Windows in order to get up and running with it quickly.

Step 1: Download and run the .msi

The .msi can be downloaded from here:
https://www.mongodb.com/download-center?jmp=nav#community

Choose the Community Server option:

The name of the file to download is:
mongodb-win32-x86_64-2008plus-ssl-3.6.3-signed.msi

While the installation is running, one of the screens will give the option to choose Complete or Custom. Choose Custom.

Then choose Browse:

This is to change the default installation location from what they recommend:

C:\Program Files\MongoDB\Server\3.6\

to

C:\mongodb

Then click Next to continue on with the installation. I chose to allow the installation of MongoDB Compass also.

 

Step 2: Create folders and run commands in the cmd line

In the new C:\mongodb folder, add a data folder and a log folder.

C:\mongodb\data
C:\mongodb\log

In the data folder, add a db folder:
C:\mongodb\data\db

Now open a command prompt in Administrator mode. cd to the mongodb\bin folder.
C:\mongodb\bin

Then type this command:
mongod --directoryperdb --dbpath C:\mongodb\data\db --logpath C:\mongodb\log\mongo.log --logappend –install

Then run this command:
net start MongoDB

Then run this command, while still in the bin directory:
mongo

Note: There may also be some warning messages in addition to what is shown in the screen shot above.

Type cls to clear the screen. Then run these commands:

show dbs

followed by ‘use’ and then the name of the database you want to create. In this case I want to create a db called ‘microblogs’.

use microblogs

The command db will show you which database you are currently in.

 

Step 3: Create user

For more information about creating a user, see:
https://docs.mongodb.com/manual/reference/method/db.createUser/

Run the following command block to create new user:

db.createUser({
	user: "chris",
	pwd: "password",
	roles: [ "readWrite", "dbAdmin" ]
});

 

Step 4: Create a collection

Run the following commands:
db.createCollection('blogs');

then

show collections

For more information about setting up MongoDB, you can follow along with the Traversy Media video here: