← Back to MongoDB Course | Chapter 8: Indexes | Lesson 7 of 7

Index strategies

Good indexing means matching indexes to your real queries, keeping few of them, and checking they are used.

In this page:

  1. Index strategies

Index strategies

Start from your slowest, most frequent queries, design compound indexes with the ESR rule, and remove unused indexes found with $indexStats.

Covered queries answer entirely from the index. Every extra index adds write cost and memory pressure, so balance both.

Monitor with the profiler.

Note: $indexStats shows which indexes are actually being used.

Example: Index strategies

bash
test> db.orders.aggregate([{ $indexStats: {} }, { $project: { name: 1, "accesses.ops": 1 } }])
[
  { name: '_id_', accesses: { ops: Long('1204') } },
  { name: 'status_1_createdAt_-1', accesses: { ops: Long('9821') } },
  { name: 'legacy_flag_1', accesses: { ops: Long('0') } }
]
test> db.orders.dropIndex("legacy_flag_1")
{ nIndexesWas: 3, ok: 1 }

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Indexing without checking real queries
  2. Keeping unused indexes
  3. Ignoring memory limits for indexes
Chapter Summary
  • Index for real query patterns
  • Use ESR for compound indexes
  • Drop unused indexes
  • Covered queries read only the index
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.