Install MongoDB on Ubuntu

28/12/2020
Chưa phân loại
In this quick post, we will see how we can install one of the most popular NoSQL database, MongoDB on Ubuntu and start using it as well. We will get started now.

MongoDB Database

MongoDB is one of the most popular NoSQL databases which is used to store and query schemaless data.

Today’s data has undefined number of properties. New properties of an object are added everyday and those properties might not be present in all the Objects which currently exist. MySQL databases store these properties even for Objects which doesn’t have them. Let’s see an example:

Name Address Line 1 Address Line 2 Address Line 3
John A-17 17th Street Florida
Sam B-46 California

If we had saved this data in a NoSQL database, it would have looked like:

[
  {
    "name" : "John",
    "address_line1" : "A-17",
    "address_line2" : "17th Street",
    "address_line3" : "Florida"
  },
  {
    "name" : "John",
    "address_line1" : "B-46",
    "address_line2" : "California"
  }
]

See the difference, the field which is not applicable for an object is not even present as a column.

Installing MongoDB

Now, installing MongoDB is just a matter of few commands. To start, let’s allow Ubuntu to ensure the authenticity of the software we are trying to install:

sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv EA312927

Once we run this command, we will get following output:

Ubuntu imported the MongoDB key into its package manager. Next, run the next command to create a list file for MongoDB:

echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse"
|sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Once we run this command, we will get following output:

Let’s finally update the package list:

sudo apt-get update

Now, we are ready to install MongoDB now:

sudo apt-get install -y mongodb-org

Once you run this command, it might take a few minutes to install MongoDB packages.
Now, run these two commands to start the MongoDB service and check its status:

sudo systemctl start mongod
sudo systemctl status mongod

Once we run this command, we will get following output:

We will also enable MongoDB to start automatically when the system starts:

sudo systemctl enable mongod

Once we run this command, we will get following output:

Queries with mongoDB

Now that we have installed and started MongoDB, we can also query data using it. Let’s try some sample commands here.

Using Mongo Shell

To start running MongoDB queries, we can open Mongo shell by just typing:

mongo

Shell will open:

Inserting Data

Now, we can make a new database:

And we can insert data into it:

Note that we didn’t have to make the platforms collection and it was made automatically.

Getting Data

We can run a simple command to get the data we saved:

In the second query above, we also printed the number of documents present in the collection.

Further Study

In this quick post, we learned how we can install MongoDB and run basic queries on it. To go deeper into MongoDB check out these excellent resources below:

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash Parameter Expansion

The parameter is used in bash to store data. Different types of data can be stored in the parameter, such as integer, string,...
29/12/2020

How to Install KDE on Manjaro Linux

I just simply can’t LOVE enough the desktop environments! Desktop environments are what makes the Linux systems lovely...
29/12/2020

Working with JSON Documents in ATOM editor

In this post we will see how we can maximize our speed and performance while working with JSON documents in ATOM text editor....
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024