Install and use MongoDB on Ubuntu

About MongoDB

MongoDB is a document-oriented NoSQL database used for high volume data storage. This is a database which came into light around the mid-2000s. It falls under the category of a NoSQL database.

To get very technical, a document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.

MongoDB Terminology

field : A name-value pair. this is a similar concept to the column of an RDBMS (Relational DB)

document : A group of fields are termed as a document. In RDBMS it's termed as a as row. MongoDB document follows JSON syntax but it is actually BSON syntax (BSON is an extended version of JSON implemented by MongoDB).

collection : A group of documents are called a collection in MongoDB. A collection is similar concept of table of an RDBMS.

database :A Physical Container for Collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases

Advantages of MongoDB

  • Schema less − MongoDB is a document database in which one collection holds different documents. Number of fields, content and size of the document can differ from one document to another.
  • Structure of a single object is clear.
  • No complex joins.
  • Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL.
  • Tuning.
  • Ease of scale-out − MongoDB is easy to scale.
  • Conversion/mapping of application objects to database objects not needed.
  • Uses internal memory for storing the (windowed) working set, enabling faster access of data.

Installation of MongoDB on Ubuntu

1. If gnupg is not installed in your server, please install it first, use the below command

sudo apt-get install gnupg

2. From the linux terminal, import the pubic key used by the package management system. MongoDB public GPG Key from

wget -qO - | sudo apt-key add

3. Add a source list for the ubuntu version using below command

echo "deb [ arch=amd64,arm64 ] bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

4. Now update ur local package database

sudo apt-get update

5. Now, install the mongodb package

  • To Install latest version
sudo apt-get install -y mongodb-org
  • To install a specific version of mongodb
sudo apt-get install -y mongodb-org=4.2.5 \ 
mongodb-org-server=4.2.5 \ 
mongodb-org-shell=4.2.5 \ 
mongodb-org-mongos=4.2.5 \

6. Start the MongoDB daemon and enable it to start on boot

sudo systemctl start mongod
sudo systemctl enable mongod

7. To verify whether the installation has completed successfully we will connect to the MongoDB database server using the mongo tool and print the connection status:

mongo --eval 'db.runCommand({ connectionStatus: 1 })'


MongoDB shell version v4.0.2
connecting to: mongodb://
MongoDB server version: 4.0.2
    "authInfo" : {
    "authenticatedUsers" : [ ],
    "authenticatedUserRoles" : [ ]
    "ok" : 1

Configuring MongoDB

8. Go to /etc/mongod.conf and enable authentication

authorization: enabled

9. Restart mongodb

sudo systemctl restart mongod

10. Now go to mongo shell by and use the admin database

use admin

11. Now create an admin user for mongodb database

    user: "mongoadmin", 
    pwd: "carhousepen334", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]

12. Now exit from mongo shell by typing


13. Now login using the admin user

mongo -u mongoAdmin -p --authenticationDatabase admin