|
| 1 | +--- |
| 2 | +description: Vibe coding guidelines and database optimization constraints for MongoDB within the backend domain. |
| 3 | +technology: MongoDB |
| 4 | +domain: backend |
| 5 | +level: Senior/Architect |
| 6 | +complexity: Advanced |
| 7 | +topic: MongoDB Database Optimization |
| 8 | +vibe_coding_ready: true |
| 9 | +version: "7.0+" |
| 10 | +tags: [database-optimization, mongodb, nosql, indexing, aggregation-pipeline, system-design, production-ready, scalable-code] |
| 11 | +ai_role: Senior MongoDB Database Architect |
| 12 | +last_updated: 2026-03-28 |
| 13 | +last_evolution: 2026-03-28 |
| 14 | +--- |
| 15 | + |
| 16 | +# ⚡ MongoDB Database Optimization Best Practices |
| 17 | + |
| 18 | +This document outlines indexing strategies (ESR Rule), aggregation pipeline optimization, and query tuning for enterprise-grade MongoDB environments. |
| 19 | + |
| 20 | +## 🎯 1. The ESR (Equality, Sort, Range) Rule |
| 21 | + |
| 22 | +When designing indexes, always follow the ESR rule to maximize efficiency. |
| 23 | + |
| 24 | +### ❌ Bad Practice |
| 25 | + |
| 26 | +Creating indexes randomly without understanding the query patterns. |
| 27 | + |
| 28 | +```javascript |
| 29 | +// A query with equality, sort, and range: |
| 30 | +// db.orders.find({ status: "shipped", amount: { $gt: 100 } }).sort({ date: 1 }) |
| 31 | + |
| 32 | +// Bad index - Range comes before Sort |
| 33 | +db.orders.createIndex({ status: 1, amount: 1, date: 1 }) |
| 34 | +``` |
| 35 | + |
| 36 | +### ✅ Best Practice |
| 37 | + |
| 38 | +Create indexes following the ESR rule: |
| 39 | +1. **E**quality fields first. |
| 40 | +2. **S**ort fields next. |
| 41 | +3. **R**ange fields last. |
| 42 | + |
| 43 | +### 🚀 Solution |
| 44 | + |
| 45 | +```javascript |
| 46 | +// Ideal index for the ESR query |
| 47 | +db.orders.createIndex({ status: 1, date: 1, amount: 1 }) |
| 48 | +``` |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## 🏗️ 2. Aggregation Pipeline Optimization |
| 53 | + |
| 54 | +Pipelines process documents in stages. Optimizing the order of these stages dramatically improves performance. |
| 55 | + |
| 56 | +### ❌ Bad Practice |
| 57 | + |
| 58 | +Filtering data after heavy transformations or sorting large un-indexed datasets. |
| 59 | + |
| 60 | +```javascript |
| 61 | +db.users.aggregate([ |
| 62 | + { $project: { name: 1, age: 1, status: 1 } }, |
| 63 | + { $sort: { age: -1 } }, |
| 64 | + { $match: { status: "active" } } |
| 65 | +]) |
| 66 | +``` |
| 67 | + |
| 68 | +### ✅ Best Practice |
| 69 | + |
| 70 | +Always use `$match` and `$sort` as early as possible in the pipeline to reduce the working set and take advantage of indexes. Use `$project` later. |
| 71 | + |
| 72 | +### 🚀 Solution |
| 73 | + |
| 74 | +```javascript |
| 75 | +db.users.aggregate([ |
| 76 | + { $match: { status: "active" } }, |
| 77 | + { $sort: { age: -1 } }, |
| 78 | + { $project: { name: 1, age: 1 } } |
| 79 | +]) |
| 80 | +``` |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +## 📉 3. Covered Queries |
| 85 | + |
| 86 | +A covered query is a query that can be satisfied entirely using an index, without having to examine the actual documents. |
| 87 | + |
| 88 | +### 🚀 Solution |
| 89 | + |
| 90 | +If you have an index on `{ status: 1, amount: 1 }`: |
| 91 | + |
| 92 | +```javascript |
| 93 | +// This is a covered query because it only projects indexed fields (and explicitly excludes _id) |
| 94 | +db.orders.find( |
| 95 | + { status: "shipped" }, |
| 96 | + { status: 1, amount: 1, _id: 0 } |
| 97 | +) |
| 98 | +``` |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +[⬆ Back to Top](#-mongodb-database-optimization-best-practices) |
0 commit comments