0
0
MongoDBquery~5 mins

Compound index and field order in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a compound index in MongoDB?
A compound index is an index on multiple fields in a MongoDB document. It helps speed up queries that filter or sort by those fields together.
Click to reveal answer
intermediate
Why does the order of fields matter in a compound index?
The order matters because MongoDB uses the index from left to right. Queries must match the prefix of the index fields to use it efficiently.
Click to reveal answer
intermediate
Given a compound index on { age: 1, name: 1 }, which queries can use this index?
Queries filtering by age alone or by both age and name can use this index. Queries filtering only by name cannot use it efficiently.
Click to reveal answer
intermediate
How does MongoDB use a compound index for sorting?
MongoDB can use a compound index to sort results if the sort order matches the index field order and direction.
Click to reveal answer
advanced
What happens if you reverse the field order in a compound index?
Reversing the field order creates a different index. Queries optimized for the original order may not benefit from the reversed index.
Click to reveal answer
Which of these is a valid compound index in MongoDB?
A{ age }
B{ age: 'ascending', name: 'descending' }
C{ name: 1 }
D{ age: 1, name: 1 }
If you have a compound index on { age: 1, name: 1 }, which query uses the index efficiently?
A{ age: 25 }
B{ name: 'Alice' }
C{ name: 'Alice', age: 25 }
D{ city: 'NY' }
What is the effect of field order in a compound index?
AIt does not matter
BMongoDB uses fields from left to right for queries
COnly the last field is used
DFields are used randomly
Can a compound index be used to sort query results?
AYes, if the sort matches the index field order
BNo, indexes are only for filtering
COnly single-field indexes can sort
DSorting is done after fetching all results
What happens if you create a compound index with reversed field order?
AMongoDB merges both indexes
BIt creates the same index
CIt creates a different index
DIt causes an error
Explain what a compound index is and why the order of fields matters in MongoDB.
Think about how MongoDB uses indexes to speed up queries.
You got /4 concepts.
    Describe how MongoDB uses a compound index to optimize queries and sorting.
    Consider both filtering and sorting when using compound indexes.
    You got /4 concepts.