AI Reading
Quick summary of this article
This guide walks you through installing MongoDB 7.0 on an AWS Ubuntu server, configuring it for remote access, and performing basic database operations like creating, reading, updating, and deleting documents. It covers everything from connecting via SSH to running CRUD commands in the Mongo shell.
- Install MongoDB by importing its official GPG key, adding the repository (use "jammy" for Ubuntu 22.04 or "focal" for 20.04), and running
sudo apt-get install -y mongodb-org. - Start and enable the MongoDB service with
sudo systemctl start mongodandsudo systemctl enable mongod, then verify it's active. - To allow remote access, change
bindIpto0.0.0.0in/etc/mongod.conf, restart the service, and open port 27017 in the AWS Security Group. - Use the Mongo shell (
mongosh) to manage data: create a database withuse, insert documents withinsertOneorinsertMany, and retrieve them withfind(). - Update documents using
updateOneorupdateManywith the$setoperator, and delete withdeleteOneordeleteManyto remove specific or all records.
Install MongoDB on AWS Ubuntu – Complete Step-by-Step Guide
In this detailed tutorial, we will learn how to install MongoDB on AWS Ubuntu server, configure it for remote access, and perform basic database operations like create, insert, update, delete, and find using the Mongo shell.
✅ Step 1: Connect to Your AWS Ubuntu Server
If you are using an AWS EC2 instance, connect to it via SSH using your private key:
ssh -i your-key.pem ubuntu@your-ec2-public-ip
Replace your-key.pem and your-ec2-public-ip with your actual key file name and EC2 IP address.
✅ Step 2: Import the MongoDB Public GPG Key
Run the following commands to add the official MongoDB repository key:
sudo apt-get install gnupg curl -y
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
✅ Step 3: Add MongoDB Repository
For Ubuntu 22.04 (Jammy), use this command:
echo "deb [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
If you are using Ubuntu 20.04, replace jammy with focal.
✅ Step 4: Install MongoDB
sudo apt-get update
sudo apt-get install -y mongodb-org
✅ Step 5: Start and Enable MongoDB
sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod
You should see active (running) status.
✅ Step 6: Verify MongoDB Installation
mongosh
Inside Mongo shell, check databases:
show dbs
✅ Step 7: Allow Remote Access (Optional)
Edit the MongoDB configuration file:
sudo nano /etc/mongod.conf
Find and replace:
bindIp: 127.0.0.1
# Change to:
bindIp: 0.0.0.0
Restart MongoDB:
sudo systemctl restart mongod
Then, in your AWS EC2 Security Group, add inbound rule for port 27017.
MongoDB Basic Operations (Database and Collections)
✅ Step 1: Create or Switch Database
use myappdb
MongoDB will switch to database myappdb. It will be created automatically when you insert data.
✅ Step 2: Create a Collection
db.createCollection("users")
You’ll get: { ok: 1 }
✅ Step 3: Insert Documents
db.users.insertOne({
name: "Umar Rahman",
email: "umar@example.com",
age: 25,
country: "India"
})
db.users.insertMany([
{ name: "Ayesha", email: "ayesha@example.com", age: 22, country: "Bangladesh" },
{ name: "Rahul", email: "rahul@example.com", age: 27, country: "India" }
])
✅ Step 4: Find Documents
db.users.find().pretty()
✅ Step 5: Update Documents
// Update one document
db.users.updateOne(
{ name: "Umar Rahman" },
{ $set: { age: 26 } }
)
// Update multiple documents
db.users.updateMany(
{ country: "India" },
{ $set: { verified: true } }
)
✅ Step 6: Delete Documents
// Delete one
db.users.deleteOne({ name: "Rahul" })
// Delete multiple
db.users.deleteMany({ country: "Bangladesh" })
// Delete all
db.users.deleteMany({})
✅ Step 7: Verify Changes
db.users.find().pretty()
That’s it! You’ve successfully installed MongoDB on AWS Ubuntu, created your first database, and learned how to insert, update, and delete documents. 🚀
Learn MongoDB for Beginners
Understand Mongo shell, CRUD operations, and indexes.
Node.js + MongoDB Integration
Learn how to connect MongoDB with Express API projects.
AWS Deployment Guide
Step-by-step tutorial for hosting apps on AWS EC2.
