Bird
Raised Fist0
MongoDBquery~10 mins

Index intersection behavior in MongoDB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Index intersection behavior
Query with multiple conditions
↓
Check available indexes
↓
Find indexes matching each condition
↓
Intersect index results
↓
Fetch documents matching all conditions
↓
Return results
When a query has multiple conditions, MongoDB finds indexes for each condition, intersects their results, and returns documents matching all conditions.
Execution Sample
MongoDB
db.collection.find({age: {$gt: 25}, city: 'NY'})
Query finds documents where age is greater than 25 and city is 'NY' using index intersection.
Execution Table
StepActionIndexes UsedIntermediate ResultExplanation
1Receive query with conditions age > 25 and city = 'NY'None yetN/AStart with query filters
2Check indexes on 'age' and 'city'Index on age, Index on cityN/AIdentify indexes that can help
3Use index on age to find docs with age > 25Index on ageDocs matching age > 25Get doc IDs matching age condition
4Use index on city to find docs with city = 'NY'Index on cityDocs matching city = 'NY'Get doc IDs matching city condition
5Intersect doc IDs from both indexesIndex on age, Index on cityDocs matching both conditionsFind docs present in both sets
6Fetch full documents for intersected IDsN/AFinal result setReturn documents matching all conditions
7Return results to clientN/AQuery completeQuery execution finished
💡 All conditions processed and intersected, final matching documents returned
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
Docs_ageN/AIDs with age > 25IDs with age > 25IDs intersectedFinal matched docs
Docs_cityN/AN/AIDs with city = 'NY'IDs intersectedFinal matched docs
Key Moments - 3 Insights
Why does MongoDB use index intersection instead of a single combined index?
MongoDB uses index intersection when no single index covers all query conditions, so it combines multiple indexes to efficiently find matching documents (see execution_table steps 2-5).
Does index intersection return documents that match only one condition?
No, index intersection returns documents that match all conditions by intersecting the sets of document IDs from each index (see execution_table step 5).
What happens if one condition has no supporting index?
MongoDB cannot use index intersection fully and may scan documents or use a single index for the other condition, reducing efficiency (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does MongoDB find documents matching the city condition?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Check the 'Action' and 'Indexes Used' columns for city condition filtering
According to variable_tracker, what does 'Docs_age' contain after Step 3?
AAll documents in the collection
BIDs of documents with city = 'NY'
CIDs of documents with age > 25
DFinal matched documents
💡 Hint
Look at the 'After Step 3' column for 'Docs_age' variable
If there was no index on 'city', how would the execution_table change?
AStep 4 would be skipped or use collection scan
BStep 3 would be skipped
CIntersection would happen earlier
DFinal results would be empty
💡 Hint
Consider what happens when an index is missing for a query condition
Concept Snapshot
Index Intersection Behavior in MongoDB:
- When a query has multiple conditions, MongoDB finds indexes for each.
- It retrieves document IDs matching each condition separately.
- Then it intersects these ID sets to find documents matching all conditions.
- This improves query efficiency when no single index covers all conditions.
- Final documents are fetched and returned after intersection.
Full Transcript
This visual execution trace shows how MongoDB handles index intersection for a query with multiple conditions. First, MongoDB receives a query filtering documents by age greater than 25 and city equal to 'NY'. It checks available indexes on these fields. Then it uses the index on age to find documents matching the age condition and separately uses the index on city to find documents matching the city condition. MongoDB intersects the document IDs from both indexes to find documents matching both conditions. Finally, it fetches the full documents for these intersected IDs and returns them as the query result. The variable tracker shows how the sets of document IDs evolve after each step. Key moments clarify why intersection is used and how it ensures all conditions are met. The quiz questions test understanding of the steps and variable states in the execution.

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

  1. Step 1: Understand the purpose of index intersection

    Index intersection allows MongoDB to use more than one index together to satisfy a query efficiently.
  2. 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.
  3. Final Answer:

    Combine multiple indexes to answer a single query -> Option A
  4. 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

  1. Step 1: Identify the method to analyze query plans

    The explain() method shows how MongoDB executes a query, including index usage.
  2. Step 2: Look for index intersection details

    Within the explain output, the indexIntersection field indicates if multiple indexes were combined.
  3. Final Answer:

    Use db.collection.find().explain() and look for indexIntersection in the plan -> Option D
  4. 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

  1. Step 1: Analyze the query filter fields

    The query filters on both age and city, and there are separate indexes on each field.
  2. Step 2: Understand MongoDB's index intersection behavior

    MongoDB can combine these two indexes to efficiently find documents matching both conditions using index intersection.
  3. Final Answer:

    Use index intersection combining both age and city indexes -> Option A
  4. 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
4. You wrote this query:
db.products.find({ category: "books", price: { $lt: 20 } })

Indexes exist on { category: 1 } and { price: 1 }. But explain() shows no index intersection used. What is a likely cause?
medium
A. MongoDB does not support index intersection on range queries
B. MongoDB prefers a single index that covers both fields if available
C. The query syntax is incorrect for index intersection
D. Index intersection only works with compound indexes

Solution

  1. Step 1: Understand MongoDB's index selection logic

    MongoDB chooses the most efficient index plan. If a single index covers both fields, it prefers that over intersection.
  2. Step 2: Consider the indexes present

    Since only single-field indexes exist, MongoDB may pick one index if it estimates it is more efficient than combining both.
  3. Final Answer:

    MongoDB prefers a single index that covers both fields if available -> Option B
  4. Quick Check:

    MongoDB picks best index, not always intersection [OK]
Hint: MongoDB picks best index plan, not always intersection [OK]
Common Mistakes:
  • Believing index intersection requires compound indexes
  • Thinking query syntax disables index intersection
  • Assuming range queries never use index intersection
5. You have a collection with indexes:
{ author: 1 }, { year: 1 }, { genre: 1 }

To optimize this query:
db.books.find({ author: "Alice", year: 2020, genre: "fiction" })

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

  1. Step 1: Understand index intersection limits

    MongoDB can combine multiple single-field indexes, but compound indexes on multiple fields are often more efficient.
  2. 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.
  3. 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.
  4. Final Answer:

    Create compound indexes on pairs like { author: 1, year: 1 } and { genre: 1 } -> Option C
  5. Quick Check:

    Compound pairs + intersection = better performance [OK]
Hint: Compound indexes on pairs plus single indexes improve intersection [OK]
Common Mistakes:
  • Assuming only single-field indexes are enough
  • Dropping indexes reduces performance
  • Believing full compound index is always best
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard