How to install and use MongoDB on Ubuntu 20.04

A NoSQL type of database, MongoDB used to store data in the form of documents similar to JSON. Due to the distinctive nature of MongoDB (to store data), it is a flexible and adaptable database management system. Apart from the strong storing mechanism, MongoDB uses its own query language named Mongo Query Language(MQL) to perform several operations on the stored data.

As MongoDB is a free and open-source database, so making it is a good choice for Linux-based systems. In today’s post, the installation and preliminary usage of MongoDB on Ubuntu 20.04 are provided.

So, without any further delay, let’s start this demonstration

How to install MongoDB on Ubuntu 20.04

Ubuntu is a well-known Debian-based distro of Linux; the steps mentioned below provide the installation path for MongoDB on Ubuntu 20.04.

Step 1: Firstly, add the GPG key, as it is required by APT to ensure the authenticity of the program being installed. To do so for MongoDB, execute the below-mentioned command :

$ wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

Note: The latest version of MongoDB is 5.0 (at the time of this writeup).

Step 2: Now, add the repository of MongoDB to our system’s repository. For this perform the following actions.

Create/Edit a mongo.list file of MongoDB inside sources.list.d directory by issuing the following command.

$ sudo nano /etc/apt/sources.list.d/mongodb.list

In that file, append the following line according to the release.

For Ubuntu 20.04:

deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse

For Ubuntu 18.04

deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/5.0 multiverse

Step 3: After that update your system’s repository with the help of the following command.

$ sudo apt update

Afterward, the command provided below helps to get MongoDB on Ubuntu.

$ sudo apt install mongodb-org

To verify the installation, you can get the version of MongoDB with the help of the following command

$ mongod --version 

Step 4 : Once the MongoDB is installed successfully, it is recommended to enable and start the Mongo service with the help of the following commands.

$ sudo systemctl enable mongod
$ sudo systemctl start mongod

Moreover, verify the mongod service status, use the below-mentioned command.

$ sudo systemctl start mongod

Finally, check the mongod service that must be active and running with enabled state.

$ sudo systemctl status mongod

How to use MongoDB on Ubuntu

In the previous sections, you have learned about the installation of MongoDB on Ubuntu. Now, we are heading towards how to use MongoDB on Ubuntu.

To access MongoDB using the mongo shell,

Write the “mongo” keyword and hit enter on Ubuntu’s terminal.

$ mongo 

You will observe that a mongo console is ready, and you can perform operations on MongoDB inside that mongo shell. The following basic operations are performed here:

Create a database

The following command(in mongo shell) will create a new database named; linuxfoss.

> use linuxfoss

Note: If the database already exists then the (use) command will switch you to that database.

Moreover, the command written below prints the list of available databases.

> show dbs

Create a collection

The documents are created inside a collection and a collection is linked with the database. We have created a new collection named <distros> by issuing the command written below.

> db.createCollection("distros")

You can get the list of available collections of a MongoDB database with the help of the following command.

> show collections 

insert document(s)

The primary task of a database is to store data and MongoDB stores data in the form of documents. The following command inserts one document in the distros collection.

> db.distros.insert([{_id: 1, Name: "Ubuntu", Family: "Debian"}, {_id: 2, Name: "LinuxMint", Family: "Ubuntu"}])

Print the Documents

The find() method in MongoDB is used to get the list of documents stored in a collection. As an example, we have extracted the content of distros collection using the command provided below.

> db.distros.find().pretty()

Note: The find() is the main method to get documents, whereas pretty() just beautifies the result.

Update Documents

The update() method in MongoDB allows you to update the documents that matches the specified condition. The command written below will update the name field of the document where the “Family” field value matches “Ubuntu“.

The second command verifies the updating process by getting the content using the find() method.

$ db.distros.update({Family: "Ubuntu"}, {$set: {Name: "Kubuntu"}})
$ db.distros.find()

Remove document

The deletion of documents in MongoDB is carried out using the remove() method. The command written below removes the documents that have “_id: 1“.

> db.distros.remove({_id: 1})

Conclusion

MongoDB is one of the widely used database management systems and it belongs to the NoSQL category of databases. The NoSQL databases do not follow the mechanism of SQL databases. This post describes the installation as well as the preliminary usage of MongoDB on Ubuntu 20.04. This guide also presents the most used MongoDB methods that are practiced to insert/delete/retrieve documents from a MongoDB database.