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?
Subscribe to our Newsletter
Join our editors every weekday evening as they steer you through the most significant news of the day, introduce you to fresh perspectives, and provide unexpected moments of joy
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:
- Amazon DynamoDB
- Azure Cosmos DB
- Google Cloud Datastore
- Oracle NoSQL Database
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
Let us get our hands dirty with 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:
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:
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
Starting your MongoDB
Execute the following command to start your MongoDB service :
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.
Checking the status of your MongoDB
To check the status of your MongoDB Service execute the following command:
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.
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.
The above command will select MongoDB’s existing admin database.
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:
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
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.
Edit the file to add the following configuration:
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:
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:
To create new database and insert a new collection(table), enter the following commands
The above command switches to the specified database. If the database doesn’t exist, it will create a new one with the specified name.
The above command will create a collection named “TestTable” with two fields “Name” and “Org” and values “Amal Nair” and “AIM”
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.
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.
To list all the documents in a collection, execute the following command
To list only the document where name field is “XXX”, execute the following command:
Dropping Documents, Collections and Databases
To remove a document based on simple field value matching condition use the following commands:
To drop an entire collection use the following command
To drop an entire database execute the following command after selecting the right database:
Congrats! You have successfully learned to set up MongoDB. Now you can confidently start your MongoDB journey.