0
0
MongoDBquery~20 mins

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

Choose your learning style9 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.