Query Optimization in MongoDB

From the MongoDB cheat sheet · Indexes & Performance · verified Jul 2026

Query Optimization

Analyze and optimize query performance

javascript
// Explain query execution
db.users.find({ 
  age: { $gte: 18 } 
}).explain("executionStats")

// Check index usage
db.users.find({ email: "john@example.com" })
  .hint({ email: 1 })  // Force specific index
  .explain()

// Get index statistics
db.users.getIndexes()
db.users.totalIndexSize()
💡 explain() shows query execution plan
📌 "COLLSCAN" means no index used (slow)
⚡ Profiler helps find slow queries
⚠️ hint() forces index but use carefully
🔗 Related: MongoDB Compass for visual analysis
performanceoptimization

More MongoDB tasks

Back to the full MongoDB cheat sheet