Skip to main content
InterviewHubPRO
Section Overview
Q14

Question 14 of 50

Part 2: Core Architecture & Implementation (Mid-Level)

28%
MongoDB & NoSQL (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)

What are Compound Indexes and Index Prefixes in MongoDB?

senior LevelMost AskedSystem DesignDatabases & SQL

In Simple Words: A compound index indexes multiple fields (e.g. { status: 1, createdAt: -1 }):

  • Index Prefixes: Subsets of the index starting from the leftmost field:
    • { status: 1 } is an index prefix (can be used by queries filtering on status).
    • { createdAt: -1 } is NOT a prefix and cannot be used alone!
  • ESR Rule (Equality, Sort, Range): Best practice for compound index field ordering:
    1. Equality fields first.
    2. Sort fields second.
    3. Range filter fields ($gt, $lt) last.