Collection scans happen when MongoDB looks through every document in a collection to find matches. This is slow and uses more resources.
Query patterns that cause collection scans in MongoDB
Introduction
Syntax
MongoDB
db.collection.find({ <query> })If the query cannot use an index, MongoDB performs a collection scan.
Indexes help MongoDB find data faster without scanning all documents.
Examples
age field, this query causes a collection scan.MongoDB
db.users.find({ age: 25 })MongoDB
db.orders.find({ status: { $not: { $eq: "shipped" } } })MongoDB
db.products.find({ name: /phone/ })region and product, this query may cause a collection scan.MongoDB
db.sales.find({ region: "west", product: "book" })Sample Program
This query searches for items in the 'toys' category. Without an index on 'category', MongoDB scans the whole collection.
MongoDB
use shop // Assume no index on 'category' db.items.find({ category: "toys" })
Important Notes
Creating indexes on frequently queried fields helps avoid collection scans.
Use explain() to check if a query uses an index or does a collection scan.
Collection scans are okay for small collections but slow down large ones.
Summary
Collection scans happen when no suitable index exists for a query.
Queries using fields without indexes or certain operators cause collection scans.
Indexes improve query speed by avoiding collection scans.
Practice
1. Which of the following query patterns in MongoDB is most likely to cause a collection scan?
easy
Solution
Step 1: Understand collection scan causes
A collection scan happens when MongoDB must check every document because no index supports the query.Step 2: Analyze query patterns
Querying a field without an index forces MongoDB to scan the entire collection, unlike queries using indexed fields.Final Answer:
Querying a field without an index -> Option AQuick Check:
No index = collection scan [OK]
Hint: No index on field? Expect collection scan [OK]
Common Mistakes:
- Assuming all queries use indexes automatically
- Confusing covered index with collection scan
- Thinking equality queries always cause scans
2. Which of the following MongoDB query syntaxes is correct and avoids collection scans by using an index on the field
age?easy
Solution
Step 1: Check query syntax correctness
All queries are syntactically valid, but some use operators that prevent index use.Step 2: Identify index-friendly query
Query with{ age: { $gt: 25 } }uses range operator on indexed field, avoiding collection scan. Queries with$whereor$regexoften cause scans.Final Answer:
db.users.find({ age: { $gt: 25 } }) -> Option AQuick Check:
Range query on indexed field = no scan [OK]
Hint: Use simple operators on indexed fields to avoid scans [OK]
Common Mistakes:
- Using $where causes collection scan
- Assuming $regex always uses index
- Confusing $text search with field index
3. Given a collection
products with an index on category, what will be the result of this query?db.products.find({ category: { $in: ['electronics', 'books'] } })medium
Solution
Step 1: Understand $in operator with indexes
The $in operator can use indexes on the queried field to find matching values efficiently.Step 2: Analyze query behavior
Sincecategoryis indexed, MongoDB uses the index to find documents where category is 'electronics' or 'books'.Final Answer:
Uses index on category and returns matching documents -> Option DQuick Check:
$in + indexed field = index used [OK]
Hint: Indexed field + $in uses index, no scan [OK]
Common Mistakes:
- Thinking $in disables index use
- Assuming $in causes syntax error
- Believing collection scan always happens
4. You have a MongoDB collection
orders with an index on status. Which query will cause a collection scan and how can you fix it?db.orders.find({ $where: 'this.status === "shipped"' })medium
Solution
Step 1: Identify $where impact on query
The $where operator runs JavaScript on each document, forcing a collection scan regardless of indexes.Step 2: Fix query to use index
Replacing $where with a direct field query{ status: 'shipped' }uses the index onstatus, avoiding collection scan.Final Answer:
Causes collection scan; fix by using { status: 'shipped' } -> Option CQuick Check:
$where disables index; direct query fixes [OK]
Hint: Avoid $where; use direct field queries for indexes [OK]
Common Mistakes:
- Assuming $where uses indexes
- Trying to fix with $regex instead of direct query
- Confusing syntax error with collection scan
5. You want to optimize this MongoDB query to avoid collection scans on a large
Given indexes on
users collection:db.users.find({ $or: [ { age: { $gt: 30 } }, { name: /John/ } ] })Given indexes on
age and name, which change will prevent collection scans?hard
Solution
Step 1: Understand $or with mixed operators
Regex queries often prevent index use, causing collection scans even if other fields are indexed.Step 2: Optimize by splitting queries
Running two separate queries--one onageand one onname--each using indexes, then combining results in application avoids scans.Final Answer:
Rewrite query to separate $or into two queries using indexed fields -> Option BQuick Check:
Split queries + indexes avoid scans [OK]
Hint: Split $or queries to use indexes separately [OK]
Common Mistakes:
- Believing regex always uses index
- Using $where increases scans
- Removing indexes reduces performance
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
