Site icon LinuxAndUbuntu

How to Install MongoDB in Ubuntu: A Step-by-Step Guide

How to install MongoDB on Ubuntu

How to install MongoDB on Ubuntu

MongoDB is a popular open-source NoSQL database system that has gained widespread adoption due to its flexibility and scalability. It is designed to handle large volumes of data and uses a document-oriented data model, which makes it easy to work with data in MongoDB.

Why MongoDB?

One of the key benefits of MongoDB is its flexibility. It can handle various data types, including unstructured, semi-structured, and structured. This makes it an ideal choice for organizations that need to work with diverse data sets.

Another benefit of MongoDB is its scalability. It can easily handle large volumes of data and can be scaled horizontally across multiple servers. This makes it ideal for applications that need to handle high traffic volumes.

MongoDB also offers a powerful query language that makes retrieving and analyzing data accessible. Its query language is similar to SQL but also supports rich document queries that enable developers to work with complex data structures.

In addition, MongoDB has a large and active community of developers and users who contribute to its ongoing development and improvement. This ensures that MongoDB is constantly evolving to meet the changing needs of its users.

One potential downside of MongoDB is that it can be more complex to set up and manage than traditional relational databases. However, this challenge can be easily overcome with the right expertise and resources.

Overall, MongoDB is a powerful and flexible NoSQL database system that offers many benefits to organizations that must work with large and diverse data sets. Its scalability, flexibility, and powerful query language make it an ideal choice for modern applications that handle large volumes of data.

What is NoSQL database?

It is important to understand how MongoDB is different from other popular DBMS such as MySQL.

MongoDB is a NoSQL database. In contrast to MySQL, the database system stores data inside documents. NoSQL databases do not offer SELECT, INSERT or UPDATE SQL queries to retrieve, insert or update data from the database. Without having to bother about building the database’s structure we can insert data in JSON format. NoSQL is also known as an unstructured database because of this aspect of its design.

MySQL users need to create database with tables and columns. Users can insert or get data as long as the given table or column is available in database.

For more detailed article on differences between MySQL and MongoDB, please refer to MongoDB Vs. MySQL.

How to Install MongoDB in Ubuntu?

Now we know something about the DMBS itself, we can now install MongoDB on Ubuntu. Let us start by updating our system using the following apt command

sudo apt update

Next we need to install the required packages for MongoDB. Here is how to do it –

sudo apt install -y libssl-dev libcurl4-gnutls-dev libicu-dev lib32gcc1 lib32stdc++6

MongoDB is available in default system repository but it might be an old version. To install the latest version we can add following MongoDB repository –

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

Update the system –

sudo apt update
sudo apt install mongodb-org

And that is it. The above command will install MongoDB on your Ubuntu server. We can now start it by using the following command –

sudo systemctl start mongod
sudo systemctl status mongod
Check mongodb status

Logging into MongoDB shell

MongoDB provides a shell to create and manage databases. We can easily login to the MongoDB shell by using the following command –

mongo
login to mongodb shell

Managing MongoDB database

As I mentioned above we do not need to create the structure of the mongoDB database. We can use use `statement` to create the document and manage collections using the createCollection method.

Create database

use myFirstDatabase

Create collections

db.createCollection("myFirstCollection")

Insert documents

We can insert data or documents into the collection we created above using the insert method.

db.myFirstCollection.insert({"username": "sandy", "userid": 324})

Update documents

We can update a document by calling the update method on the collection. The update method accepts two objects. The first object is the object we want to update and the second object is the new data.

db.myFirstCollection.update({"username": "sandy"}, {"username": "sandy013", "userid": 321})

Delete documents

db.myFirstCollection.remove({"username": "sandy013"})

So this is how we can perform basic operations in MongoDB database. I strongly recommend reading my article MongoDB vs. MySQL to better understand the database and its usage in different projects.

Conclusion

So there you have it. In this article we learn how to install MongoDB on Ubuntu. If you are using Ubuntu derivative, the steps should be similar. If you encounter any problems, let me know in the comment section below.

Exit mobile version