Index intersection is when MongoDB combines multiple indexes to answer a query instead of using just one index. This helps improve query performance when no single index covers all query conditions.
Click to reveal answer
intermediate
How does MongoDB decide to use index intersection?
MongoDB's query planner evaluates available indexes and may choose to intersect them if it estimates that combining indexes will be faster than using a single index or scanning the collection.
Click to reveal answer
intermediate
Can MongoDB intersect any number of indexes for a query?
No, MongoDB supports index intersection using up to 2 indexes for a single query.
Click to reveal answer
beginner
What types of queries benefit most from index intersection?
Queries with multiple conditions on different fields that each have their own index benefit most. MongoDB can intersect these indexes to efficiently filter documents matching all conditions.
Click to reveal answer
intermediate
Does index intersection always improve query performance?
Not always. Sometimes using a single compound index or scanning the collection is faster. MongoDB's query planner chooses the best approach based on data statistics.
Click to reveal answer
What does MongoDB do when it uses index intersection?
ACombines multiple indexes to answer a query
BUses only the first index it finds
CScans the entire collection
DCreates a new index on the fly
✗ Incorrect
Index intersection means MongoDB combines multiple indexes to efficiently answer a query.
How many indexes can MongoDB intersect for a single query?
A1
B3
C2
DUnlimited
✗ Incorrect
MongoDB supports index intersection using up to 2 indexes for a single query.
Which query type benefits most from index intersection?
AQueries with multiple conditions on different indexed fields
BQueries with a single condition
CQueries without any indexes
DQueries that update documents
✗ Incorrect
Queries with multiple conditions on different indexed fields benefit most from index intersection.
Does index intersection always guarantee the fastest query?
AYes, always
BOnly for queries without filters
COnly for aggregation queries
DNo, sometimes a single index or collection scan is faster
✗ Incorrect
MongoDB's query planner chooses the best approach; index intersection is not always fastest.
What role does the query planner have in index intersection?
AIt forces index intersection for all queries
BIt decides whether to use index intersection based on estimated performance
CIt disables index intersection
DIt creates new indexes automatically
✗ Incorrect
The query planner evaluates indexes and decides if index intersection will improve query speed.
Explain what index intersection is and when MongoDB uses it.
Think about how MongoDB can use more than one index to answer a query.
You got /4 concepts.
Describe the limitations and benefits of index intersection in MongoDB.
Consider both what index intersection can and cannot do.
You got /4 concepts.
Practice
(1/5)
1. What does index intersection in MongoDB allow the database to do?
easy
A. Combine multiple indexes to answer a single query
B. Create a new index automatically
C. Merge two collections into one
D. Delete unused indexes to save space
Solution
Step 1: Understand the purpose of index intersection
Index intersection allows MongoDB to use more than one index together to satisfy a query efficiently.
Step 2: Differentiate from other index operations
It does not create or delete indexes, nor merge collections; it only combines existing indexes for query optimization.
Final Answer:
Combine multiple indexes to answer a single query -> Option A
Quick Check:
Index intersection = combining indexes [OK]
Hint: Index intersection means using multiple indexes together [OK]
Common Mistakes:
Thinking index intersection creates new indexes
Confusing index intersection with collection merging
Assuming it deletes indexes automatically
2. Which of the following is the correct way to check if MongoDB used index intersection for a query?
easy
A. Check the size of the indexes with db.collection.indexSize()
B. Run db.collection.stats() to see index usage
C. Use db.collection.aggregate() without any options
D. Use db.collection.find().explain() and look for indexIntersection in the plan
Solution
Step 1: Identify the method to analyze query plans
The explain() method shows how MongoDB executes a query, including index usage.
Step 2: Look for index intersection details
Within the explain output, the indexIntersection field indicates if multiple indexes were combined.
Final Answer:
Use db.collection.find().explain() and look for indexIntersection in the plan -> Option D
Quick Check:
Explain() shows index intersection [OK]
Hint: Use explain() to see index intersection in query plan [OK]
Common Mistakes:
Using stats() which shows collection info, not query plan
Running aggregate() without explain() does not show index use
Checking index size does not reveal intersection
3. Given a collection with indexes on { age: 1 } and { city: 1 }, what will MongoDB do for this query?
db.users.find({ age: 30, city: "NY" })
medium
A. Use index intersection combining both age and city indexes
B. Use only the index on city
C. Use only the index on age
D. Perform a full collection scan ignoring indexes
Solution
Step 1: Analyze the query filter fields
The query filters on both age and city, and there are separate indexes on each field.
Step 2: Understand MongoDB's index intersection behavior
MongoDB can combine these two indexes to efficiently find documents matching both conditions using index intersection.
Final Answer:
Use index intersection combining both age and city indexes -> Option A
Quick Check:
Query on two indexed fields = index intersection [OK]
Hint: Multiple single-field indexes on query fields trigger index intersection [OK]
Common Mistakes:
Assuming MongoDB uses only one index per query
Thinking MongoDB always does full scan if multiple indexes exist
Confusing index intersection with compound indexes
Which index strategy will most likely improve performance using index intersection?
hard
A. Create a compound index on { author: 1, year: 1, genre: 1 }
B. Keep single-field indexes and let MongoDB intersect them
C. Create compound indexes on pairs like { author: 1, year: 1 } and { genre: 1 }
D. Drop all indexes and create a single index on { author: 1 }
Solution
Step 1: Understand index intersection limits
MongoDB can combine multiple single-field indexes, but compound indexes on multiple fields are often more efficient.
Step 2: Evaluate index strategies
A full compound index on all three fields is best but costly to maintain; compound indexes on pairs plus a single index can help MongoDB intersect efficiently.
Step 3: Compare options
Create compound indexes on pairs like { author: 1, year: 1 } and { genre: 1 } creates compound indexes on pairs, enabling better intersection than only single-field indexes, improving query speed without full compound index overhead.
Final Answer:
Create compound indexes on pairs like { author: 1, year: 1 } and { genre: 1 } -> Option C