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:

NameAddress Line 1Address Line 2Address Line 3
JohnA-1717th StreetFlorida
SamB-46California

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

Understanding vm.swappiness

The Linux kernel is a rather complex piece of software with a long list of components such as modules, interfaces and configuration...
Chưa phân loại
29/12/2020

Hướng dẫn cài đặt và cấu hình Squid HTTP Proxy Server trên CentOS 7 8 / RHEL 7 ubuntu

Giới thiệu Squid là một phần mềm proxy server dùng cho việc lưu trữ cache và lọc nội dung web. Squid hỗ...
23/12/2020

List Files Sorted by Size in a Directory Tree on Linux

Most file systems sort the files in alphabetical order of names. A user might, however, have some other preferences and...
29/12/2020