Ubuntu 24.04 (Noble Numbat) is officially available for download and installation on any supported hardware for production use. This NoSQL database type is created for high scalability and flexibility. MongoDB stores its data in JSON-like document structure. This makes it easy to manipulate the data even when it contains complex structures. The flexible data format of MongoDB is attractive to developers working on various programming languages.
Some of the popular applications of MongoDB are:
- In mobile applications development
- Data storage for analytics in IoT
- Some Content Management Systems use MongoDB by default
- Development of Social media applications
- For real-time data analytics and processing
- Building of e-commerce platforms
- When storing any kind of semi and unstructured data
In this article we are going to do an installation of MongoDB on Ubuntu 24.04 Linux system.
Update System and Add repository
We being the installation by updating our OS repository list.
sudo apt update
If you desire so all installed packages can be updated on the system.
sudo apt upgrade -y
For any kernel updates applied on the system a reboot is required.
[ -e /var/run/reboot-required ] && sudo reboot
Wait for the system to come online if it’s rebooted then login again and import MongoDB repo key.
sudo apt install gnupg curl
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
Add MongoDB repository into your Ubuntu system.
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Run the following command to update your system local package database:
sudo apt update
Confirm there are not printed errors.
Hit:1 https://mirror.hetzner.com/ubuntu/packages noble InRelease
Ign:2 https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 InRelease
Hit:3 https://mirror.hetzner.com/ubuntu/packages noble-updates InRelease
Hit:4 https://mirror.hetzner.com/ubuntu/packages noble-backports InRelease
Hit:5 https://mirror.hetzner.com/ubuntu/security noble-security InRelease
Get:6 https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 Release [2,090 B]
Get:7 https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 Release.gpg [866 B]
Get:8 https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0/multiverse amd64 Packages [38.7 kB]
Get:9 https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0/multiverse arm64 Packages [37.6 kB]
Fetched 79.2 kB in 2s (41.7 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
Install MongoDB Server on Ubuntu 24.04
One the repository has been added and confirmed to be working, installation of MongoDB Server on Ubuntu 24.04 can be done. While in your active terminal session, execute the following commands.
sudo apt -y install mongodb-org
Confirm the installation was successful by checking the software version.
$ mongod --version
db version v7.0.9
Build Info: {
"version": "7.0.9",
"gitVersion": "3ff3a3925c36ed277cf5eafca5495f2e3728dd67",
"openSSLVersion": "OpenSSL 3.0.13 30 Jan 2024",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "ubuntu2204",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
The MongoDB data directory is /var/lib/mongodb
and logs are stored inside the /var/log/mongodb
directory.
Configurations can be customized on the following file.
sudo vim /etc/mongod.conf
Service can be started manually by running the commands below.
sudo systemctl start mongod
To restart use:
sudo systemctl restart mongod
And to stop the service execute:
sudo systemctl stop mongod
To ensure MongoDB service will be started at system reboot, run:
sudo systemctl enable mongod
Check error logs using:
sudo tail -f /var/log/mongodb/mongod.log
Using MongoDB Server
Start using MongoDB by initialing mongosh
session
# mongosh
Current Mongosh Log ID: 663406e5292bc66b3a2202d7
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5
Using MongoDB: 7.0.9
Using Mongosh: 2.2.5
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
------
The server generated these startup warnings when booting
2024-05-02T21:33:22.361+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2024-05-02T21:33:23.055+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------
test>
Let’s create a sample database called Movies
test> use Movies
switched to db Movies
Movies>
Check currently selected database
Movies> db
Movies
MongoDB will only create a database when you first store data in it. The data could be a collection or a document.
Adding Data to the database
You can add a document to your database using db.collection.insert() command.
Movies> db.movies.insert({name: "Dune: Part Two", year: 2024})
{
acknowledged: true,
insertedIds: { '0': ObjectId('66340a3690f41b1d482202da') }
}
Movies> db.movies.insert({name: "Challengers", year: 2024})
{
acknowledged: true,
insertedIds: { '0': ObjectId('66340a9b90f41b1d482202db') }
}
To list all created databases , use the following command:
Movies> show dbs
Movies 152.00 KiB
admin 40.00 KiB
config 92.00 KiB
local 40.00 KiB
Use db.dropDatabase()
command to drop an existing database.
Movies> show dbs
Movies 152.00 KiB
admin 40.00 KiB
config 92.00 KiB
local 40.00 KiB
test> use Movies
switched to db Movies
Movies>
Movies> db.dropDatabase()
{ ok: 1, dropped: 'Movies' }
Now check list of databases.
Movies> show dbs
admin 40.00 KiB
config 92.00 KiB
local 40.00 KiB
We can confirm the database was deleted.
Reference documentation: