Tutorial Lengkap MongoDB: Database NoSQL untuk Aplikasi Modern
MongoDB adalah database NoSQL document-oriented yang sangat populer untuk aplikasi modern. Dengan schema yang fleksibel dan kemampuan scaling horizontal, MongoDB cocok untuk berbagai use case dari web applications hingga big data analytics.
Apa itu MongoDB?
MongoDB menyimpan data dalam format dokumen JSON-like (BSON) yang memberikan:
- Schema flexibility: Tidak perlu mendefinisikan struktur data sebelumnya
- Horizontal scaling: Mudah di-scale dengan sharding
- High performance: Optimized untuk read/write operations
- Rich query language: Support complex queries dan aggregations
- Native replication: Built-in high availability
- Content management systems
- Real-time analytics
- Mobile applications
- IoT data storage
- E-commerce platforms
- Social media applications
Instalasi MongoDB
1. Install di 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 dan 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 dengan mongosh
docker exec -it mongodb mongosh -u admin -p password123
3. MongoDB Atlas (Cloud)
Untuk production, MongoDB Atlas menyediakan managed service:
# Connection string format
connectionstring = "mongodb+srv://username:password@cluster.mongodb.net/dbname"
MongoDB Shell (mongosh)
1. Basic Commands
// Connect ke 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 dengan 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 dan 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(