Table of contents
1.
Introduction
2.
Downloading MongoDB
3.
Configuration
4.
Running the MongoDB Server
5.
Environment Variables Setup
6.
Setup on Ubuntu
6.1.
Start, Stop and Restart the Server
7.
An analogy to RDBM Systems:
8.
Frequently Asked Questions
9.
Key Takeaways
Last Updated: Mar 27, 2024

Setting up MongoDB

Author Gunjeev Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A Database Management System (DBMS) is a system software used to create, manage and work with databases. MongoDB is a non-relational database management system. Unlike SQL(Structured Query Language) databases based on tables, MongoDB is a document-based database. MongoDB is often an integral part of the Javascript web ecosystem, MERN, or MEAN stack. 

You can learn more about MERN stack web development here!


In this blog, we will learn the MongoDB setup procedure.

Downloading MongoDB

To download the current version of MongoDB, refer to this link. Once you download the installer, you are now ready to do the MongoDB setup. All you need to do is follow along with the step-by-step process of the installer wizard. 

 

MongoDB Installation Wizard

 

Installation Wizard Finish Setup

 

Once the setup has finished, we must configure the MongoDB setup to use it efficiently. To do so, head over to the folder where you have the program files installed. For Windows users, by default, the MongoDB setup folder would be the C: Drive. 

 

The path for the correct folder on a standard windows machine is:

C: -> Program Files -> MongoDB

 

Inside this folder, head over to the bin directory. By default, its path would be:

C: -> Program Files -> MongoDB -> Server -> 5.0(version) -> bin

Configuration

In the bin directory, you will find some files. Let us understand what they mean.

The two files we need to look into are the two executable files named mongo and mongod.

  • mongod: The mongod executable file is responsible for running the server, managing memory, accepting incoming requests, etc. It is the primary background process that Mongo uses. 
  • mongo: The mongo file is a command-line interface (CLI). It is the service that developers use to interact with MongoDB.

Running the MongoDB Server

To run the Mongo server, we first need to make specific directories that Mongo needs to store all database data. To make our work more accessible, we will make these directories in the default paths of Mongo so that we do not need to change the configuration settings. 

Essentially we need to make two folders. Firstly make a folder called "data" inside the C: drive. Next, create a folder called "db" inside the newly created folder data.

To sum it up, we need the following directory tree:

C: -> data -> db

 

Once this directory setup is complete, head to the bin folder inside the MongoDB folder and open a shell there. On the command prompt, write:

mongod

 

Done! The MongoDB server is now up and running. The MongoDB setup is now complete!

To interact with the server, we need a mediator. This is where the mongo CLI comes into play. Open another terminal, go to the bin directory and run mongo.

mongo

 

This would establish a connection between the two open terminals. The mongo terminal is what we would be using. On the mongo terminal, run the following command:

db

 

The output should be:

The output of the command

 

'test' is the result of the command because currently, we have a test database by default.

 

To check the version of MongoDB installed on the machine, just run mongo --v in the bin directory of MongoDB.

mongo –v

Environment Variables Setup

To avoid following tedious setup procedures for every database created, it is recommended to set up environment variables on your machine when you do MongoDB setup. This can be done through the terminal or the menus. 

 

On a Windows machine, the procedure is as follows:

Go to Advanced System Settings, select the Environment Variables menu, choose the Path option and click edit. Simply paste the path of your bin folder here! 

Advanced System Settings -> Environment Variables -> Path -> Edit

 

Setup on Ubuntu

To setup MongoDB on Ubuntu or other Debian based Linux distros, you can follow this step-by-step procedure:

 

1. Import the MongoDB Public GPC key:

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

 

2. Create a MongoDB list file in the apt folder:

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

 

3. Update the repository

sudo apt-get update

 

4. Install MongoDB 

apt-get install mongodb-10gen = 4.2

 

Make sure you install the latest version. 

Start, Stop and Restart the Server

Now that we have completed the MongoDB setup on Ubuntu let us look at the commands to run or stop the server.

 

1. Start the Server:

sudo service mongodb start

 

2. Stop the Server:

sudo service mongodb stop

 

3. Restart the Server:

sudo service mongodb restart

An analogy to RDBM Systems:

In traditional Relational Database Management Systems, data is stored in databases with rows and columns tables. MongoDB is a NOSQL DBMS. In NOSQL DBMS, data is stored in a BSON format. A BSON format is simply a binary version of JSON. 

Data is stored in the form of collections. If we create a diagrammatic analogy between SQL tables and NOSQL collections, we would have the following analogy:

 

Comparison between NOSQL and SQL DBMS

 

In every NOSQL collection, BSON Documents exist that have single data storages called fields. 

Also Read - Cardinality In DBMS

Frequently Asked Questions

1. What is the Compass?
Just like we have discussed how to operate MongoDB using the mongo CLI, there are a lot of GUI alternatives as well. One of them is the Compass Application.

 

2. What is the use of db.stats()?
The db.stats() method returns a document containing the open database's current usage statistics. This usually includes information on storage in bytes.

 

3. What is the use of db.help?
The db.help() method is a command that returns all the valid commands related to the mongoDB client.

 

4. What is MongoDB Atlas?
MongoDB Atlas is a fully functional and end-to-end managed cloud database facility. It is a service that Mongo provides to its users to derail them from the tedious procedures of deployment, management, and healing. 

Key Takeaways

Now that we are done with setting up MongoDB, you all are already on your way to becoming professional MERN stack developers. This blog covered the MongoDB setup from scratch. To continue your Mongo journey, you can refer to the following blogs: Introduction To Mongo

Live masterclass