MongoDB Tutorial 0/120 lessons ~6 min read Lesson 63
Compound Indexes
Compound indexes cover multiple fields in a specific order.
Course progress0%
Focus
3 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
Compound indexes cover multiple fields in a specific order. The order is critical: the index can satisfy queries on a prefix of the keys — the 'ESR rule' (Equality, Sort, Range) decides the order.
Syntax reference
js
db.orders.createIndex({ userId: 1, createdAt: -1 });// Covers: userId=X, userId=X sort createdAt desc, etc.
Best practices
- Apply the ESR rule: Equality → Sort → Range.
- Re-use one compound index for many query shapes.
Ready to mark this lesson complete?Track your journey across the entire course.