MongoDB Indexes: Compound, Sparse, Partial, TTL
Fast recall
Quick revision
Core answer
partialFilterExpression picks exactly which documents to index (active users only, unshipped orders). TTL indexes auto-delete documents after expireAfterSeconds, perfect for sessions and log data. In Spring Data MongoDB you declare them with @Indexed, @CompoundIndex, or programmatically via MongoTemplate.indexOps().Whether you can choose the right index type for an access pattern and understand the ESR rule and trade-offs
Confusing sparse with partial indexes, ignoring the ESR rule on compound-index field ordering, or assuming TTL deletes immediately
Explain the ESR rule, name the difference between sparse and partial indexes, cover multikey limits (only one array field in a compound), and reference `explain('executionStats')`
Interview-ready guide
Interview answer
Explain the index model
- B-tree structures that avoid collection scans
- Without them, every query is O(N)
Cover the core types
- Compound + ESR: Equality, Sort, Range order
- Sparse: skip docs missing the field (also sparse-unique trick)
- Partial: stricter filter expression — prefer this for new indexes
- TTL: auto-expire after
expireAfterSeconds, single-field date - Multikey: automatic on arrays, one array per compound index
Mention the costs
- Every index slows writes
- Unused indexes still pay the tax — audit with
$indexStats - Use
explain('executionStats')to confirm IXSCAN and the key-to-doc ratio
Wrap up with Spring Data
@Indexed,@CompoundIndex, orMongoTemplate.indexOps()programmatic- Use migration scripts for reviewable index changes in production
MongoDB Indexes: Compound, Sparse, Partial, TTL
Direct Answer
MongoDB indexes are B-tree structures that let the query planner skip collection scans. Compound indexes cover multiple fields and follow the ESR rule (Equality → Sort → Range) so the leading fields are the most selective. Sparse indexes include only documents where the field exists — great for optional fields and for sparse-unique constraints. Partial indexes are stricter: a partialFilterExpression picks exactly which documents to index (active users only, unshipped orders). TTL indexes auto-delete documents after expireAfterSeconds, perfect for sessions and log data. In Spring Data MongoDB you declare them with @Indexed, @CompoundIndex, or programmatically via MongoTemplate.indexOps().
Common Mistakes & Pitfalls
Confusing sparse with partial indexes, ignoring the ESR rule on compound-index field ordering, or assuming TTL deletes immediately
Check your understanding
Answer these without looking back at the article.
- Why is partial index usually preferable to sparse index for new designs?
- How do you identify and drop unused indexes safely in production?
- When would you use a hashed index outside of sharding?
- How does the TTL monitor interact with replica-set secondaries?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.