Reading Time: 7 minutes

What is MongoDB?

mongodb logo

MongoDB is a documented database management system that does not require the description of the table scheme. It is an example of NoSQL systems (NoSQL - this is an approach to implement scalable db storage with a flexible data model), MongoDB uses JSON like documents and a database scheme. It is often used in web-development along with Big data. MongoDB is written in C++, C, and JavaScript. It is also cross-platform, which lets us deploy it on multiple platforms.  

One of the advantages of MongoDB over SQL-relational databases is that any relational database (e.g., MySQL) has a standard scheme that shows the number of tables and connections between them, and there is no such connection between tables in MongoDB.

MongoDB has a collection or a group of documents. The document is a collection of key-value pairs. Documents have a dynamic scheme. A dynamic scheme means that documents from the same collection do not have to have the same set of fields or structures; also, a common field of documents can include data of various types. 

This article will provide more information on MongoDB, what it is, and where it can be used, also look through its advantages and see how to install it. 

Advantages of MongoDB

  • Absence of a schema
  • Based on the collections of various documents
  • A clear structure of every object
  • Highly scalable
  • Internal memory is used to store data, which lets us get data faster.
  • Data is stored as JSON objects.
  • MongoDB supports document-based queries

MongoDB Use Cases

  • Storage of events
  • Content and documents management
  • Mobile apps database
  • Games database
  • Storing monitoring and sensor data
  • Storage of web-pages data

MongoDB's Administration Tools

  • mongo - interactive administration shell
  • mongostat - command-line tool
  • mongotop - for monitoring
  • mongoimport and mongoexport - tools of export and import from and to JSON
  • mongodump and mongorestore - tools to create a backup copy and restore databases from it

Prerequisites 

  • A Ubuntu 18.04 Server
  • Be the root user
  • Access to sudo if not the root user

Installation on Ubuntu

First, let’s begin by updating our Ubuntu server.

root@host:~# apt update && apt -y upgrade
Hit:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:2 http://us.archive.ubuntu.com/ubuntu bionic InRelease
Hit:3 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:4 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@host:~#

At the time of writing the article, MongoDB 3.6 in the official repository in Ubuntu. We can check that by running the following command.

root@host:~# apt-cache show mongodb | grep -i version
Version: 1:3.6.3-0ubuntu1.1
Version: 1:3.6.3-0ubuntu1
root@host:~#

Latest Version

However, the latest official stable release of MongoDB is 4.4. To install and use the latest stable version, we need to introduce a special repository of MongoDB packages into the apt repository. We’re going to begin by importing the GPG key for the latest stable version of MongoDB.

 root@host:~# curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -
 OK
 root@host:~# 

In case the version has changed, and we need to add another key, the required key is found on the official server of MongoDB keys. You can find the file using the .asc extension. Here’s the link to use for that file.

We have used the curl tool — a command-line tool used to transfer data; It is commonly used in many OSes.

Let’s check whether the key was added properly running the following command: 

root@host:~# apt-key list
/etc/apt/trusted.gpg
--------------------
pub rsa4096 2019-05-28 [SC] [expires: 2024-05-26]
      2069 1EEC 3521 6C63 CAF6 6CE1 6564 08E3 90CF B1F5
uid [ unknown] MongoDB 4.4 Release Signing Key <packaging@mongodb.com>

root@host:~#

Download MongoDB

Now, we need to point the package installer where to find the MongoDB package to download and install, using the following command:  

root@host:~# echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list
deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse
root@host:~#

Run the Update

Next, we will update the package manager so that the new information will take effect, and apt can download and install MongoDB.

root@host:~# apt update
Hit:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:2 http://us.archive.ubuntu.com/ubuntu bionic InRelease
Get:3 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Ign:5 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 InRelease
Get:6 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 Release [5,391 B]
Get:7 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 Release.gpg [801 B]
Get:8 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4/multiverse arm64 Packages [4,332 B]
Get:9 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4/multiverse amd64 Packages [6,074 B]
Fetched 180 kB in 2s (101 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
root@host:~#

Install

Now we can install MongoDB using the following command.

root@host:~# apt install mongodb-org -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  mongodb-database-tools mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server
  mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
  mongodb-database-tools mongodb-org mongodb-org-database-tools-extra mongodb-org-mongos
  mongodb-org-server mongodb-org-shell mongodb-org-tools
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 104 MB of archives.
After this operation, 200 MB of additional disk space will be used.
Get:1 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4/multiverse amd64 mongodb-database-tools amd64 100.2.1 [54.5 MB]
Get:2 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4/multiverse amd64 mongodb-org-shell amd64 4.4.1 [13.2 MB]
Get:3 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4/multiverse amd64 mongodb-org-server amd64 4.4.1 [20.3 MB]
Get:4 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4/multiverse amd64 mongodb-org-mongos amd64 4.4.1 [15.7 MB]
Get:5 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4/multiverse amd64 mongodb-org-database-tools-extra amd64 4.4.1 [5,608 B]
Get:6 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4/multiverse amd64 mongodb-org-tools amd64 4.4.1 [2,892 B]
Get:7 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4/multiverse amd64 mongodb-org amd64 4.4.1 [3,520 B]
Fetched 104 MB in 10s (10.4 MB/s)
Selecting previously unselected package mongodb-database-tools.
(Reading database ... 105638 files and directories currently installed.)
Preparing to unpack .../0-mongodb-database-tools_100.2.1_amd64.deb ...
Unpacking mongodb-database-tools (100.2.1) ...
Selecting previously unselected package mongodb-org-shell.
Preparing to unpack .../1-mongodb-org-shell_4.4.1_amd64.deb ...
Unpacking mongodb-org-shell (4.4.1) ...
Selecting previously unselected package mongodb-org-server.
Preparing to unpack .../2-mongodb-org-server_4.4.1_amd64.deb ...
Unpacking mongodb-org-server (4.4.1) ...
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack .../3-mongodb-org-mongos_4.4.1_amd64.deb ...
Unpacking mongodb-org-mongos (4.4.1) ...
Selecting previously unselected package mongodb-org-database-tools-extra.
Preparing to unpack .../4-mongodb-org-database-tools-extra_4.4.1_amd64.deb ...
Unpacking mongodb-org-database-tools-extra (4.4.1) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../5-mongodb-org-tools_4.4.1_amd64.deb ...
Unpacking mongodb-org-tools (4.4.1) ...
Selecting previously unselected package mongodb-org.
Preparing to unpack .../6-mongodb-org_4.4.1_amd64.deb ...
Unpacking mongodb-org (4.4.1) ...
Setting up mongodb-org-shell (4.4.1) ...
Setting up mongodb-org-database-tools-extra (4.4.1) ...
Setting up mongodb-database-tools (100.2.1) ...
Setting up mongodb-org-mongos (4.4.1) ...
Setting up mongodb-org-tools (4.4.1) ...
Setting up mongodb-org-server (4.4.1) ...
Adding system user `mongodb’ (UID 111) ...
Adding new user `mongodb' (UID 111) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb’ (GID 117) ...
Done.
Adding user `mongodb’ to group `mongodb’...
Adding user mongodb to group mongodb
Done.
Setting up mongodb-org (4.4.1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
root@host:~# 

Verify Installation

Next, let’s verify the installation using the version flag.

root@host:~# mongod --version
db version v4.4.1
Build Info: {
    "version": "4.4.1",
    "gitVersion": "ad91a93a5a31e175f5cbf8c69561e788bbc55ce1",
    "openSSLVersion": "OpenSSL 1.1.1 11 Sep 2018",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu1804",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}
root@host:~#

Configure MongoDB

We have installed MongoDB, but it still needs to be configured for it to work properly. To accomplish this, we need to configure systemd to start MongoDB automatically to run in the background. Let’s run the systemctl command to start the MongoDB service.

root@host:~# systemctl start mongod.service
root@host:~#

Check Status

Now, let’s check the status of MongoDB

root@host:~# systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-11-16 14:55:31 UTC; 35s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 12274 (mongod)
   CGroup: /system.slice/mongod.service
           └─12274 /usr/bin/mongod --config /etc/mongod.conf

Nov 16 14:55:31 host systemd[1]: Started MongoDB Database Server.
root@host:~# 

Enable Service

Since the service is running, let’s enable it to start after a restart of the server.

root@host:~# systemctl enable mongod
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.
root@host:~#

Run Diagnostics

Now let’s verify our settings by connecting to MongoDB and run a diagnostic command. This command will also enter the server address and port: 

root@host:~# mongo --eval 'db.runCommand({ connectionStatus: 1 })'
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("6c138394-e3a4-43fb-9b56-c3e3720a228d") }
MongoDB server version: 4.4.1
{
        "authInfo" : {
                "authenticatedUsers" : [ ],
                "authenticatedUserRoles" : [ ]
        },
        "ok" : 1
}
root@host:~#

Because our command returned status “ok”: 1 - this means that the installation of MongoDB was completed successfully and is ready to work. Keep in mind that MongoDB uses port 27017 and local address 127.0.0.1. 

MongoDB Commands

It’s also essential to know some commands for managing MongoDB: 

systemctl status mongod - shows the status of MongoDB;

systemctl stop mongod - stops MongoDB;

systemctl start mongod - starts MongoDB; 

systemctl restart mongod - restarts MongoDB;

systemctl disable mongod - prevents MongoDB from starting automatically; 

systemctl enable mongod - enables MongoDB to start automatically.

Create Database

Next, let’s create a db. First, we need to switch to the mongo shell using the following command. 

root@host:~# mongo --shell
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8ff0cc20-9008-43b1-9942-ba256b3ca914") }
MongoDB server version: 4.4.1
type "help" for help
Welcome to the MongoDB shell.
For interactive help, type “help.”
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
---
The server generated these startup warnings when booting:
        2020-11-16T14:55:31.459+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2020-11-16T14:55:31.865+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        Enable MongoDB’s free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc.).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>

And running the use command, we will create a db, liquidwebTestDB


switched to db liquidwebTestDB
>
> use liquidwebTestDB

“Switched to” means that the db was created. To get the name of the current db, the db command is used. 

> db
liquidwebTestDB
>

List Databases

To get a list of all the databases, the show dbs command should be used.

> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>

Insert Record

As we can see, our db is not on the list. To see it there, we need to make at least one record in the new database. We can do that by running the insert command. This command adds records to a MongoDB database.

db.liquidwebTestDB.insert({“name”:”Katherine“})

> db.liquidwebTestDB.insert({"name":"Katherine"})
WriteResult({ "nInserted" : 1 })
>

Relist Databases

Now let’s check the database list once again.

> show dbs
admin 0.000GB
config 0.000GB
liquidwebTestDB 0.000GB
local 0.000GB
>

Done! The database was created successfully. 

Conclusion

In this article, we provided information on MongoDB, its advantages and uses, as well as the main command-line tools. Also, we learned how to add a repository, download, and install the latest version; configure the start of the service. 

Learn More

We pride ourselves on being The Most Helpful Humans In Hosting™!

Our Support Teams are filled with experienced Linux technicians and talented system administrators who have intimate knowledge of multiple web hosting technologies, especially those discussed in this article.

Should you have any questions regarding this information, we are always available to answer any inquiries with issues related to this article, 24 hours a day, 7 days a week 365 days a year.

If you are a Fully Managed VPS server, Cloud Dedicated, VMWare Private Cloud, Private Parent server, Managed Cloud Servers, or a Dedicated server owner and you are uncomfortable with performing any of the steps outlined, we can be reached via phone at @800.580.4985, a chat or support ticket to assisting you with this process.

Avatar for Katherine Kelly

About the Author: Katherine Kelly

Latest Articles

In-place CentOS 7 upgrades

Read Article

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article