Bird
Raised Fist0
MongoDBquery~20 mins

Query patterns that cause collection scans in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Collection Scans
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Which query causes a collection scan due to missing index?
Given a collection users with an index on age, which query will cause a collection scan?
MongoDB
db.users.find({name: 'Alice'})
Adb.users.find({age: 30})
Bdb.users.find({age: 30, name: 'Alice'})
Cdb.users.find({name: 'Alice'})
Ddb.users.find({age: {$gt: 25}})
Attempts:
2 left
💡 Hint
Think about which field has an index and which does not.
❓ query_result
intermediate
2:00remaining
Which query pattern causes a collection scan despite an index?
Assuming an index exists on status, which query will still cause a collection scan?
MongoDB
db.orders.find({status: {$ne: 'shipped'}})
Adb.orders.find({status: {$in: ['shipped', 'pending']}})
Bdb.orders.find({status: 'shipped'})
Cdb.orders.find({status: {$eq: 'shipped'}})
Ddb.orders.find({status: {$ne: 'shipped'}})
Attempts:
2 left
💡 Hint
Think about how negation operators affect index usage.
📝 Syntax
advanced
2:00remaining
Which query syntax causes a runtime error due to invalid operator usage?
Identify the query that will cause a runtime error in MongoDB due to incorrect operator usage.
Adb.products.find({price: {$between: [50, 100]}})
Bdb.products.find({$and: [{price: {$gt: 50}}, {price: {$lt: 100}}]})
Cdb.products.find({price: {$gte: 50, $lte: 100}})
Ddb.products.find({price: {$lt: 100}})
Attempts:
2 left
💡 Hint
Check if the operator is valid in MongoDB query language.
❓ optimization
advanced
2:00remaining
Which query pattern can be optimized to avoid collection scan?
Given a collection with an index on category, which query can be rewritten to use the index and avoid a collection scan?
Adb.items.find({category: {$exists: true}})
Bdb.items.find({category: {$in: ['books', 'electronics']}})
Cdb.items.find({category: {$ne: null}})
Ddb.items.find({category: {$not: {$eq: 'clothing'}}})
Attempts:
2 left
💡 Hint
Consider which operators allow index usage.
🧠 Conceptual
expert
3:00remaining
Why does a query with a regex pattern cause a collection scan?
Consider a collection with an index on the username field. Why does the query db.users.find({username: /admin/}) cause a collection scan?
ABecause MongoDB can only use indexes for regex queries if the pattern is a prefix without special characters.
BBecause the regex pattern is anchored at the start, so index is not used.
CBecause regex queries never use indexes in MongoDB.
DBecause the index on username is corrupted.
Attempts:
2 left
💡 Hint
Think about how MongoDB uses indexes with regex patterns.

Practice

(1/5)
1. Which of the following query patterns in MongoDB is most likely to cause a collection scan?
easy
A. Querying a field without an index
B. Querying a field with a single-field index
C. Using an indexed field with equality operator
D. Using a covered index query

Solution

  1. Step 1: Understand collection scan causes

    A collection scan happens when MongoDB must check every document because no index supports the query.
  2. Step 2: Analyze query patterns

    Querying a field without an index forces MongoDB to scan the entire collection, unlike queries using indexed fields.
  3. Final Answer:

    Querying a field without an index -> Option A
  4. Quick 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
A. db.users.find({ age: { $gt: 25 } })
B. db.users.find({ $where: 'this.age > 25' })
C. db.users.find({ age: { $regex: '^2' } })
D. db.users.find({ $text: { $search: '25' } })

Solution

  1. Step 1: Check query syntax correctness

    All queries are syntactically valid, but some use operators that prevent index use.
  2. Step 2: Identify index-friendly query

    Query with { age: { $gt: 25 } } uses range operator on indexed field, avoiding collection scan. Queries with $where or $regex often cause scans.
  3. Final Answer:

    db.users.find({ age: { $gt: 25 } }) -> Option A
  4. Quick 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
A. Throws a syntax error due to $in usage
B. Performs a collection scan and returns all documents
C. Returns no documents because $in is unsupported
D. Uses index on category and returns matching documents

Solution

  1. Step 1: Understand $in operator with indexes

    The $in operator can use indexes on the queried field to find matching values efficiently.
  2. Step 2: Analyze query behavior

    Since category is indexed, MongoDB uses the index to find documents where category is 'electronics' or 'books'.
  3. Final Answer:

    Uses index on category and returns matching documents -> Option D
  4. Quick 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
A. Syntax error; fix by removing $where
B. Uses index; no fix needed
C. Causes collection scan; fix by using { status: 'shipped' }
D. Causes collection scan; fix by adding $regex

Solution

  1. Step 1: Identify $where impact on query

    The $where operator runs JavaScript on each document, forcing a collection scan regardless of indexes.
  2. Step 2: Fix query to use index

    Replacing $where with a direct field query { status: 'shipped' } uses the index on status, avoiding collection scan.
  3. Final Answer:

    Causes collection scan; fix by using { status: 'shipped' } -> Option C
  4. Quick 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 users collection:
db.users.find({ $or: [ { age: { $gt: 30 } }, { name: /John/ } ] })

Given indexes on age and name, which change will prevent collection scans?
hard
A. Remove indexes and rely on collection scan for speed
B. Rewrite query to separate $or into two queries using indexed fields
C. Use $where with JavaScript to filter documents
D. Change regex to case-insensitive without anchors

Solution

  1. Step 1: Understand $or with mixed operators

    Regex queries often prevent index use, causing collection scans even if other fields are indexed.
  2. Step 2: Optimize by splitting queries

    Running two separate queries--one on age and one on name--each using indexes, then combining results in application avoids scans.
  3. Final Answer:

    Rewrite query to separate $or into two queries using indexed fields -> Option B
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard