0
0
MongoDBquery~10 mins

Compound index and field order in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Compound index and field order
Create compound index with fields
MongoDB stores index entries
Query uses index fields in order
Efficient query if prefix fields match
Query uses index to speed up search
If order mismatched, index not used fully
This flow shows how MongoDB creates a compound index with fields in a specific order and how queries use that order to efficiently find data.
Execution Sample
MongoDB
db.collection.createIndex({ age: 1, name: 1 })
db.collection.find({ age: 25, name: "Alice" })
Create a compound index on age and name, then query documents matching both fields.
Execution Table
StepActionIndex StateQuery ConditionIndex Usage
1Create compound index on {age:1, name:1}Index entries sorted by age then nameNoneIndex ready
2Query with {age:25, name:'Alice'}Index unchangedage=25 and name='Alice'Full index used for search
3Query with {age:25}Index unchangedage=25Index used on age prefix only
4Query with {name:'Alice'}Index unchangedname='Alice'Index NOT used efficiently
5Query with {name:'Alice', age:25}Index unchangedname='Alice' and age=25Index NOT used efficiently
6End of demonstrationIndex unchangedNo more queriesExecution stops
💡 Queries stop after demonstrating index usage with different field orders
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
IndexNoneCompound index on {age:1, name:1}SameSameSameSame
Query ConditionNone{age:25, name:'Alice'}{age:25}{name:'Alice'}{name:'Alice', age:25}None
Index UsageNoneFull index usedPartial index usedNot used efficientlyNot used efficientlyNone
Key Moments - 2 Insights
Why does the query {name:'Alice'} not use the compound index efficiently?
Because the compound index is ordered with age first, MongoDB can only use the index efficiently if the query includes the first field (age). Querying only by name skips the first field, so the index is not used fully (see execution_table row 4).
What happens if the query fields are reversed like {name:'Alice', age:25}?
The query does not use the index efficiently. The order of fields in the query document does not affect index usage; MongoDB uses the index prefix order (age then name). Since the query does not specify the first field alone or as a prefix, the index cannot be used fully (see execution_table row 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the query use the full compound index?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Index Usage' column for 'Full index used'
According to the variable tracker, what is the index usage after querying {age:25}?
AFull index used
BIndex not used
CPartial index used
DIndex rebuilt
💡 Hint
Look at 'Index Usage' after Step 3 in variable_tracker
If the compound index was created as {name:1, age:1}, which query would use the index efficiently?
A{age:25, name:'Alice'}
B{name:'Alice'}
C{age:25}
D{name:'Alice', age:25}
💡 Hint
Index usage depends on the first field in the index order
Concept Snapshot
Compound index syntax: createIndex({field1: order, field2: order})
Order matters: MongoDB uses index efficiently only if query matches prefix fields in order
Query with first field missing cannot use index fully
Queries matching prefix fields use index to speed up search
Field order affects which queries benefit from the index
Full Transcript
This visual execution shows how MongoDB compound indexes work with field order. First, a compound index is created on age and name. The index stores entries sorted by age first, then name. Queries that include age first can use the index fully, speeding up search. Queries that only specify name do not use the index efficiently. The execution table traces queries with different conditions and shows how index usage changes. The variable tracker records the index state, query conditions, and index usage after each step. Key moments clarify why field order matters and how it affects query performance. The visual quiz tests understanding of index usage at different steps and how changing field order affects queries. The snapshot summarizes the key rules for compound indexes and field order in MongoDB.