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?
✗ Incorrect
Compound indexes specify multiple fields with sort order (1 for ascending, -1 for descending). Option D is correct.
If you have a compound index on { age: 1, name: 1 }, which query uses the index efficiently?
✗ Incorrect
Queries filtering on the first field (age) or both fields can use the index. Filtering only by name cannot.
What is the effect of field order in a compound index?
✗ Incorrect
MongoDB uses the compound index fields in the order they are defined, from left to right.
Can a compound index be used to sort query results?
✗ Incorrect
Compound indexes can support sorting if the sort order matches the index field order.
What happens if you create a compound index with reversed field order?
✗ Incorrect
Reversing the field order creates a different compound index with different query optimization.
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.