| by Arround The Web | No comments

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

MongoDB is well-known NoSQL database management system. As a document oriented database management system, it does not need that users structure the database before inserting data. You’ll see how to install MongoDB on Ubuntu in this article.

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 on 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
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
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.

The post How to Install MongoDB in Ubuntu: A Step-by-Step Guide appeared first on LinuxAndUbuntu.

Share Button

Source: LinuxAndUbuntu

Leave a Reply