MongoDB
Overview
MongoDB is a document database used for storing application data with flexible schemas.
Deployment
Kubernetes Resources
| Resource |
Name |
Namespace |
| Deployment |
mongodb |
databases |
| Service |
mongodb |
databases |
| PVC |
mongodb-data |
databases |
Configuration
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb
namespace: databases
spec:
replicas: 1
template:
spec:
containers:
- name: mongodb
image: mongo:7
ports:
- containerPort: 27017
volumeMounts:
- name: data
mountPath: /data/db
Access
Connection Details
| Property |
Value |
| Host |
mongodb.databases.svc |
| Port |
27017 |
| Protocol |
MongoDB Wire |
Connection String
Databases
| Database |
Purpose |
| hub |
Hub application data |
| admin |
Administrative |
| local |
Local operations |
Collections
Hub Database
| Collection |
Description |
| users |
User profiles |
| sessions |
Active sessions |
| configurations |
App settings |
Queries
Basic Operations
// Find documents
db.collection.find({ field: "value" })
// Insert document
db.collection.insertOne({ field: "value" })
// Update document
db.collection.updateOne(
{ _id: ObjectId("...") },
{ $set: { field: "new value" } }
)
// Delete document
db.collection.deleteOne({ _id: ObjectId("...") })
Aggregation Pipeline
db.collection.aggregate([
{ $match: { status: "active" } },
{ $group: { _id: "$category", count: { $sum: 1 } } },
{ $sort: { count: -1 } }
])
Indexes
Creating Indexes
// Single field index
db.collection.createIndex({ field: 1 })
// Compound index
db.collection.createIndex({ field1: 1, field2: -1 })
// Text index for search
db.collection.createIndex({ content: "text" })
Index Recommendations
| Collection |
Fields |
Type |
| users |
email |
Unique |
| sessions |
userId, createdAt |
Compound |
| logs |
timestamp |
TTL |
Authentication
Users
| User |
Database |
Roles |
| admin |
admin |
root |
| hubUser |
hub |
readWrite |
Creating User
db.createUser({
user: "appUser",
pwd: "password",
roles: [
{ role: "readWrite", db: "mydb" }
]
})
Backup
mongodump
mongodump \
--uri="mongodb://user:pass@mongodb:27017" \
--out=/backup/mongodb \
--gzip
mongorestore
mongorestore \
--uri="mongodb://user:pass@mongodb:27017" \
--gzip \
/backup/mongodb
Monitoring
Key Metrics
| Metric |
Description |
| connections.current |
Active connections |
| opcounters.query |
Queries/sec |
| opcounters.insert |
Inserts/sec |
| mem.resident |
Memory usage |
Health Check
db.runCommand({ serverStatus: 1 })
db.runCommand({ dbStats: 1 })
Query Optimization
- Use indexes for frequent queries
- Limit returned fields with projection
- Use explain() to analyze queries
- Avoid large document arrays
Explain Plan
db.collection.find({ field: "value" }).explain("executionStats")
Troubleshooting
Common Issues
| Issue |
Cause |
Resolution |
| Slow queries |
Missing index |
Add appropriate index |
| Connection refused |
Auth failure |
Check credentials |
| Out of memory |
Large documents |
Increase resources |
| Lock timeout |
Write contention |
Optimize writes |
Logs
kubectl logs -n databases deployment/mongodb