In this tutorial, we'll discuss a guide to install MongoDB on Ubuntu 24.04 and managing your first database.
MongoDB is an open-source, document-oriented NoSQL database that provides high performance, high availability, and easy scalability. Unlike traditional relational databases, MongoDB uses a flexible schema that allows you to store data in JSON-like documents, making it ideal for handling unstructured data or rapidly changing data structures.
MongoDB's document model is intuitive and powerful, enabling developers to store related data together, query it efficiently, and scale seamlessly as their application grows. As a leading database choice for modern applications, MongoDB is widely used in various industries, including finance, healthcare, and e-commerce, for tasks ranging from simple data storage to complex analytics.
This guide walks you through the installation of MongoDB Community Edition on an Ubuntu system and introduces the basic operations you need to get started with managing your data. Whether you're building a small-scale application or a large, distributed system, MongoDB offers the tools and flexibility required to handle the demands of modern data-driven environments.
Install MongoDB on Ubuntu
Prerequisites:
- A Ubuntu 24.04 installed dedicated server or KVM VPS.
- A root user access or normal user administrative privileges.
- Basic Linux command knowledge.
Step 1: Update the System
Before installing any software, it's essential to update the package index on your Ubuntu system.
sudo apt update
sudo apt upgrade -y
Step 2: Install MongoDB Community Edition
(For latest version visit the official MongoDB documentation page)
Import the MongoDB GPG Key. MongoDB provides an official GPG key to verify the authenticity of the packages. You can import it using the following command:
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
Next, add the MongoDB repository to your system's sources list. Create a new file using a text editor like nano:
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
Now, update the package index and install the latest stable version of MongoDB:
sudo apt update
sudo apt install -y mongodb-org
Once the installation is complete, start the MongoDB service and enable it to start on boot:
sudo systemctl start mongod
sudo systemctl enable mongod
You can verify that MongoDB is running by checking its status:
sudo systemctl status mongod
Optional. Although you can specify any available version of MongoDB, apt-get will upgrade the packages when a newer version becomes available. To prevent unintended upgrades, you can pin the package at the currently installed version:
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-database hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-mongosh hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections
Step 3: Access MongoDB Shell
MongoDB comes with an interactive shell that allows you to manage the database. To access it, type:
mongosh
This will open the MongoDB shell where you can execute commands.
Step 4: Create a Database
In MongoDB, databases are created dynamically when you insert data. However, you can create a database explicitly by switching to a new database:
use mydatabase
Replace mydatabase
with your desired database name. MongoDB will switch to this database, and if it doesn’t exist, it will be created.
Step 5: Create a Collection (Table Equivalent)
Collections in MongoDB are equivalent to tables in relational databases. You can create a collection by inserting a document into it:
db.createCollection("mycollection")
Replace mycollection
with your desired collection name. However, MongoDB will automatically create a collection when you insert a document if it doesn’t exist.
Step 6: Insert Data into the Collection
To insert a document into the collection, use the insertOne method:
db.mycollection.insertOne({ name: "John Doe", age: 30, city: "New York" })
This command inserts a single document into the mycollection collection with fields name, age, and city.
Step 7: Query the Collection
To retrieve data from the collection, you can use the find method:
db.mycollection.find()
This command returns all documents in the mycollection collection. You can also use queries to filter the data:
db.mycollection.find({ age: 30 })
This command returns all documents where the age field is 30.
Step 8: Update a Document
You can update an existing document using the updateOne method:
db.mycollection.updateOne({ name: "John Doe" }, { $set: { age: 31 } })
This command updates the age field to 31 for the document where the name is "John Doe".
Step 9: Delete a Document
To delete a document, use the deleteOne method:
db.mycollection.deleteOne({ name: "John Doe" })
This command deletes the document where the name is "John Doe".
Conclusion
We've seen a guide to install MongoDB on Ubuntu 24.04 and managing your first database, created a database, added a collection, inserted data, and performed basic CRUD operations. MongoDB is a powerful NoSQL database, and this tutorial should give you a solid foundation to start working with it.