intermediateTop 30 Scenario-Based Questions
A MongoDB collection has a compound index on {status:1, createdAt:-1} but queries with only createdAt are slow. Why?
Leftmost prefix rule: a compound index on {status:1, createdAt:-1} supports: WHERE status = X (uses leftmost prefix). WHERE status = X AND createdAt = Y (uses both). But NOT: WHERE createdAt = Y (skips status — index not used). Fix: 1) Create a separate index on createdAt: db.orders.createIndex({createdAt:-1}). 2) Or if queries span both patterns, create two separate indexes. 3) Rule: equality con
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response