Complete MongoDB Tutorial: NoSQL Database for Modern Applications
MongoDB is a highly popular document-oriented NoSQL database for modern applications. With flexible schema and horizontal scaling capabilities, MongoDB is suitable for various use cases from web applications to big data analytics.
What is MongoDB?
MongoDB stores data in JSON-like documents (BSON) providing:
- Schema flexibility: No need to define data structure beforehand
- Horizontal scaling: Easy to scale with sharding
- High performance: Optimized for read/write operations
- Rich query language: Support for complex queries and aggregations
- Native replication: Built-in high availability
- Content management systems
- Real-time analytics
- Mobile applications
- IoT data storage
- E-commerce platforms
- Social media applications
Installing MongoDB
1. Install on Ubuntu
# Import MongoDB public GPG key
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
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
Update and install
sudo apt update
sudo apt install -y mongodb-org
Start MongoDB
sudo systemctl start mongod
sudo systemctl enable mongod
Verify installation
mongosh --eval 'db.runCommand({ connectionStatus: 1 })'
2. Install via Docker
# Pull MongoDB image
docker pull mongo:7.0
Run MongoDB container
docker run -d \
--name mongodb \
-p 27017:27017 \
-e MONGOINITDBROOTUSERNAME=admin \
-e MONGOINITDBROOTPASSWORD=password123 \
-v mongodbdata:/data/db \
mongo:7.0
Connect with mongosh
docker exec -it mongodb mongosh -u admin -p password123
3. MongoDB Atlas (Cloud)
For production, MongoDB Atlas provides a managed service:
# Connection string format
connectionstring = "mongodb+srv://username:password@cluster.mongodb.net/dbname"
MongoDB Shell (mongosh)
1. Basic Commands
// Connect to MongoDB
mongosh
// Show databases
show dbs
// Use/create database
use myapp
// Show collections
show collections
// Current database
db
// Database stats
db.stats()
// Drop database
db.dropDatabase()
2. CRUD Operations
Create (Insert):// Insert single document
db.users.insertOne({
name: "Ruby Abdullah",
email: "ruby@example.com",
age: 30,
skills: ["Python", "JavaScript", "MongoDB"],
address: {
city: "Jakarta",
country: "Indonesia"
},
createdAt: new Date()
})
// Insert multiple documents
db.users.insertMany([
{ name: "Alice", email: "alice@example.com", age: 25 },
{ name: "Bob", email: "bob@example.com", age: 35 },
{ name: "Charlie", email: "charlie@example.com", age: 28 }
])
Read (Query):
// Find all documents
db.users.find()
// Find with filter
db.users.find({ age: { $gt: 25 } })
// Find one document
db.users.findOne({ email: "ruby@example.com" })
// Projection (select fields)
db.users.find({}, { name: 1, email: 1, id: 0 })
// Sort
db.users.find().sort({ age: -1 }) // Descending
// Limit and skip (pagination)
db.users.find().skip(10).limit(5)
// Count
db.users.countDocuments({ age: { $gt: 25 } })
Update:
// Update one document
db.users.updateOne(
{ email: "ruby@example.com" },
{ $set: { age: 31 } }
)
// Update many documents
db.users.updateMany(
{ age: { $lt: 30 } },