How to install and use MongoDB on Debian 11

MongoDB is a widely used NoSQL category thus a non-relational type of database. MongoDB does not follow the SQL mechanism to store and retrieve data. The traditional database management systems store data in tabular form whereas MongoDB stores data in JSON documents. MongoDB deals with unstructured data and provides a well-structured mechanism for manipulating the data.

MongoDB is a free and open-source database management system. Thus, it is suitable for Linux-based operating systems. The Linux systems can be availed of by getting any distribution that is based on the Linux kernel. Debian is one of the most used distributions of Linux and there are tens of distributions that are derived from Debian.

In this article, a sequential guide is provided to install MongoDB on Debian 11(Bullseye).

How to install MongoDB on Debian 11 (Bullseye)

Here, you will find the steps that are necessary to get MongoDB on your Debian 11 system:

Step 1 : Firstly, import the GPG key of MongoDB with the help of the following command. The GPG key is required by APT to authenticate the Debian(or any program) being installed. To do so, the command described below can be practiced to get the GPG key:

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

Note : At the time of installation, 5.0 is the latest version of MongoDB. You can get the GPG key of any version from the project site.

Step 2 : You have to add the Debian repository to your system.

For that, create a source.list file of MongoDB in /etc/apt/sources.list.d/  and add the link to the repository of MongoDB. The below-stated command assisted us in getting the file and adding link to MongoDB’s repo:

$ echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-5.0.list

Note : You can get the latest repository by navigating to repo.mongodb.org.

Step 3 : After performing the first two steps, update the Debian systems repository with the help of the following command.

$ sudo apt update

And install MongoDB by issuing the following command.

$ sudo apt install mongodb-org

Once the package is installed, verify the installation with the help of the following command.

$ mongod --version 

Step 4 : It is recommended to enable the mongodb service as it assists in getting the service ready after every reboot. To do so, execute the following command.

$ sudo systemctl enable --now mongod

Check the status of the service with the help of below mentioned command.

$ sudo systemctl status mongod

How to use MongoDB on Debian 11

This section contains some basic operations that are performed on any database. To start using MongoDB on Debian 11, you have to access the mongo shell with the help of the following command.

$ mongo

After accessing the mongo console, we are now heading to perform the following operations: insert/retrieve/delete/update.

  • Create a Database and Collection

You need a database and its collection to start using MongoDB: Firstly,  we have created a database named linuxfoss.

> use linuxfoss

Note : For instance, if the database already exists with the same name. Then, instead of creating a duplicate, the above-mentioned command will switch you to that database.

The command provided here creates a collection named staff in our linuxfoss database.

> db.createCollection("staff")
  • Insert documents

Firstly, you need some data inside MongoDB to start performing operations on that data. As MongoDB stores data in form of documents, so the following command will add two documents in staff collection of linuxfoss database. 

> db.staff.insert([{_id:1, name:"John"}, {_id:2, name:"Alen"}])
  • Retrieve documents

To retrieve documents, the find() method of MongoDB is used. We have experienced getting the data from staff collection by issuing the command stated below:

> db.staff.find()
  • Update documents

Update is another primary operation that is performed in databases. As we are using staff(as a collection name) and linuxfoss(as a database name), so the following query will update the name of the document that has id field value equals to “1“. 

> db.staff.update({_id:1}, {$set :{name:"Sam"}})

Verify the change by getting the content of staff collection and it can be seen that

> db.staff.find()
  • Remove documents

For removal, MongoDB provides support of the remove() method. From the staff’s collection, the below provided mongo command will remove the document of id=2:

> db.staff.remove({_id: 2})

MongoDB provides extensive support for manipulating documents. However, the methods provided here are the basis of any database management system.

How to remove MongoDB from Debian 11

Although MongoDB provides distinctive functionality as compared to traditional database management systems. However, if you are planning to remove MongoDB from your Debian system, you can use the following command.

$ sudo apt remove mongodb-org

Conclusion

MongoDB being a NoSQL category of databases has emerged as a widely used DBMS around the globe. The reason for their emergence is the way MongoDB deals with unstructured data and can store as well as perform several operations on the stored data. This post provides a demonstration of installing and using MongoDB on Debian. By following, you can avail latest version of MongoDB on Debian. We have also performed several basic operations of the database using the mongo shell. If you are going to choose the database management system, it is recommended that you must try MongoDB. 

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×