MITB Banner

A Complete Kickstart Guide To MongoDB

Share

Everything we see inside a gadget or an electronic device – including televisions, smartphones or even personal computers – is nothing but data. Due to the expansion of digital technology, we have been generating a lot of data. According to IDC, the Global Datasphere is expected to reach a gigantic 175 zettabytes by 2025. This is equivalent to over 175 x 1,000,000,000,000 Gigabytes. However, all this data may not be stored, but considering even 10% of this huge amount is still massive.

In a short time, data has also become the backbone of the so called AI revolution, and scientists worldwide are working on making machines intelligent using nothing but data. But raw data is not useful. It needs to pre pre-processed and refined to make it valuable.

So how do we store and process such a huge amount of data?

Enter NoSQL

Less than a couple of decades ago, Relational DataBases were the undisputed champions in storing and handling data. Oracle DB, MySQL, MSSQL and many others were popular among developers and organizations. As the size of data began to expand, Relation Databases struggled to cope due to their highly structured tabular format and incapacity to scale well.

NoSQL, on the other hand, exploited the unstructured nature of the data itself. NoSQL databases can be used across large distrusted systems and are highly scalable and much faster at handling large amounts of data than traditional relational databases.

Popular NoSQL DataBases

Given below are some of the popular NoSQL Databases:

  • MongoDB
  • Redis
  • Couchbase
  • Amazon DynamoDB
  • Cassandra
  • Azure Cosmos DB
  • Google Cloud Datastore
  • Oracle NoSQL Database

Enter MongoDB

MongoDB is an open-source cross-platform document-oriented database system developed and supported by MongoDB Inc and initially released in 2009. MongoDB stores data as JSON-like documents and is written in C++, Go, JavaScript and Python. Since its release, MongoDB has become one of the most popularly used NoSQL dataBase systems due to its ease of use and efficiency.

Here are some of the pros of using MongoDB

  • Wide support across platforms and programming languages
  • Supports indexing
  • Supports field, range query, and regular-expression searches
  • Scales horizontally using sharding
  • High availability using replica sets
  • Supports the use of JavaScript in queries, aggregation functions and can be sent directly to the database for execution

Let us get our hands dirty with MongoDB !

HandsOn MongoDB

Installing MongoDB Community Edition

In this section, we will learn to set up  a complete MongoDB environment. The following example is done on an Amazon AWS EC2 Linux instance.

Open up your Linux terminal and shoot the following block of commands to install MongoDB in your system. The following commands will install MongoDB v4.2:

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add - && \
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list && \
sudo apt-get update && \
sudo apt-get install -y mongodb-org

If you receive an error indicating that gnupg is not installed, you can install gnupg using the command below: 

sudo apt-get install gnupg

Once it is installed successfully, repeat the first block of commands.

To prevent unintended upgrades of MongoDB, you can pin the package at the currently installed version by executing the following commands:

echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

You have now successfully installed MongoDB.

Important directories to consider 

The following directories are important while trying to configure or debug.

MongoDB lib : /var/lib/mongodb
MongoDB Log : /var/log/mongodb
MongoDB Configuration : /etc/mongod.conf

MongoDB Service

Starting your MongoDB 

Execute the following command to start your MongoDB service :

sudo systemctl start mongod

If you receive the following error message :

      Failed to start mongod.service: Unit mongod.service not found.

Run the following command first and then use the start command.

sudo systemctl daemon-reload

Checking the status of your MongoDB 

To check the status of your MongoDB Service execute the following command:

sudo systemctl status mongod

If the service is running, it will show an active symbol with a green dot.

Enabling MongoDB service to ensure it starts after a system reboot

We can now enable MongoDB as a system service to ensure continuous availability. This will automatically start the database after a system reboot.

sudo systemctl enable mongod

Other useful Commands :

Stopping MongoDB : sudo systemctl stop mongod

Restarting MongoDB Service : sudo systemctl restart mongod

For detailed instructions specific to your OS follow the official MongoDB installation documentation here.

Entering Mongo Shell

After starting the MongoDB service, execute the command mongo to enter inside the mongo shell.

Creating An Admin User

Now the first thing to do is to have a MongoDB user. We will create an admin user who has admin privileges across databases. To create an admin user, execute the following sequence of codes inside the mongo shell.

use admin

The above command will select MongoDB’s existing admin database.

db.createUser(
  {
    user: "myadmin",
    pwd: passwordPrompt(),
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

Execute the above command and enter the password when prompted.The function will create a new admin user called “myadmin” with the specified privileges.

Granting Root permissions

Root privilege allows a user to delete databases. To grant root privilege to a user, execute the following commands:

use admin
db.grantRolesToUser('myadmin',[{ role: "root", db: "admin" }])

The command show users will display all the users and their roles.

Exit the mongo shell by typing and entering the “exit” command.

Entering Mongo as Administrator

Execute the following commands to enter the mongo shell as an admin user . Enter the password when prompted.

mongo --port 27017  --authenticationDatabase "admin" -u "myadmin" -p

Enter exit command to exit the shell.

For more information on enabling access control, follow the detailed instructions here.

Configuring Mongodb For Remote Access

Open the default configuration file for Mongo. You can find it at the default location :  /etc/mongod.conf. Execute the following command to open the configuration file in vim editor.

sudo vim /etc/mongod.conf

Edit the file to add the following configuration:

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,  <your server IP here(private IP for AWS instances)>
#security
security:
  authorization: 'enabled'
# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

Note:

In the network interface section, append your server IP address. (Private IP for AWS instances)

After making the changes, press esc followed by a “:wq” and enter to save the file and exit.

For the changes to take effect, restart the MongoDB service using the following command:

sudo systemctl restart mongod

Important Note:

To be able to access the MongoDB server remotely you must make the port externally available. AWS users can edit the security groups to add inbound rules to the MongoDB port (27017). 

Congrats !! You can now access your MongoDB remotely. Lets test the connection using a popular MongoDB GUI tool called Robo3T

Connecting your MongoDB server Robo3T 

Robo 3T is a free, lightweight, open-source MongoDB GUI. To download Robo3T head to https://robomongo.org/download and click download, select the application based on your OS.Once downloaded, install and open the application.

On opening the application, you will see the following window. Click on create to create a new MongoDB connection.

Enter a name for the connection. In the address field, enter your Mongo Server IP address.(If your Database resides in an AWS EC2 instance use the public IP or Elastic IP of the instance). Click on the Authentication tab to enter the authentication details.

Enter the username and password you provided while setting up the MongoDB and click on the test button.

If the connection is successful you will see the below window. If you receive an error, make sure that enetres credentials and IP address are correct. Also, make sure if the MongoDB server port is accessible.

On successful testing, save the Connection for future use.

Now, you can connect to the database server by selecting the connection and clicking the connect button. This will open up the connection and will display all the databases in the server on the left side panel.

Initially, the database server will have only two databases by default – system and config. We will come back once we create a new database using the shell.

Creating Databases, Collections and Documents

Let’s go back to our MongoDB shell. Open up the terminal an authenticate as admin user using the following command:

mongo --port 27017  --authenticationDatabase "admin" -u "myadmin" -p

In the mongo shell, type the following command to list all the available databases:

Show dbs

To create new database and insert a new collection(table), enter the following commands

use TestDataBase

The above command switches to the specified database. If the database doesn’t exist, it will create a new one with the specified name.

db.TestTable.insert({"Name":"Amal Nair", "Org":"AIM"})

The above command will create a collection named “TestTable” with two fields “Name” and “Org” and values “Amal Nair” and “AIM”

The show collections command will list all the collections(tables) in a database.If the above command is successful, show dbs command would now have “TestDataBase” listed.

Now if we go back to Robo3T and connect to the database server, we will be able to see the above database and collection.

Note:

We can also create databases, collections and documents using Robo3T GUI. Right-click on a database and select create database option to create a new database. Right-click on collections and select create collection option to create a new collection. To insert documents enter the insert query in the query field and execute.

Querying From Collections

Before we query something lets add some more documents(items or rows) into our TestTable.

use TestDataBase
db.TestTable.insert({"Name":"XXX", "Org":"Google"})
db.TestTable.insert({"Name":"YYY", "Org":"Microsoft"})
.
.

To list all the documents in a collection, execute the following command

db.collection_name.find()

I.e,

db.TestTable.find()

To list only the document where name field is “XXX”, execute the following command:

db.TestTable.find({"Name":"XXX"})

Dropping Documents, Collections and Databases

To remove a document based on simple field value matching condition use the following commands:

use TestDataBase
db.TestTable.remove({"Name" : "Amal Nair"})

To drop an entire collection use the following command

db.collection_name.drop()

I.e,

db.TestTable.drop()

To drop an entire database execute the following command after selecting the right database:

use database_name
db.dropDatabase()

I,e.

use TestDataBase
db.dropDatabase()

Congrats! You have successfully learned to set up MongoDB. Now you can confidently start your MongoDB journey.

Share
Picture of Amal Nair

Amal Nair

A Computer Science Engineer turned Data Scientist who is passionate about AI and all related technologies. Contact: amal.nair@analyticsindiamag.com
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.