MongoDB known as Mongo, is an open-source document database that is used in a wide variety of modern online applications. It is categorised as a NoSQL database because it does not use the usual table-based relational database structure.
MongoDB uses JSON-like documents with dynamic schemas, which implies that, unlike relational databases, MongoDB does not require a schema to be defined before data can be added to a database. You can alter the schema whenever and as frequently as you want without having to build a new database with the revised structure.
MongoDB package available in Ubuntu is not managed by MongoDB and we will follow the steps below to install a version managed by MongoDB
Step 1) We will add MongoDB repository key to our local apt keyring
$ wget -qO – https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add –
Step 2) In this step we will add MongoDB repository to systems list of sources by editing the file
$ sudo vi /etc/apt/sources.list
To the end of the file, add in the MongoDB source.
deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu/ focal/mongodb-org/5.0 multiverse
Note: You can visit the link https://repo.mongodb.org/apt/ubuntu/dists/focal/mongodb-org/ to download the version you would want to install.
Step 3) Update system with the new repository added
$ sudo apt update
Step 4) Setup MongoDB in your system
$ sudo apt install -y mongodb-org
Step 5) Begin MongoDB
Start the mongo daemon using the command
$ sudo systemctl start mongod
To reload mongo daemon related issues, execute the command below
$ sudo systemctl daemon-reload
To check if mongo is working as expected, run
$ sudo systemctl status mongod
To enable mongo daemon to start automatically in each boot, run
$ sudo systemctl enable mongod
Other useful commands related to mongod service
$ sudo systemctl [status|start|stop|restart|enable|disable] mongod
To start mongo from the shell, type
$ mongo
To connect to mongo and to show the version of the installed mongo, run
$ mongo –eval ‘db.runCommand({ connectionStatus: 1 })’