0
0
MongoDBquery~20 mins

Combining logical and comparison operators in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logical Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents matching combined conditions
Given a collection products with fields price and category, which query returns all products with price greater than 100 and category either 'electronics' or 'appliances'?
MongoDB
db.products.find({ $and: [ { price: { $gt: 100 } }, { category: { $in: ['electronics', 'appliances'] } } ] })
Adb.products.find({ price: { $gte: 100 }, category: { $nin: ['electronics', 'appliances'] } })
Bdb.products.find({ $or: [ { price: { $gt: 100 } }, { category: { $in: ['electronics', 'appliances'] } } ] })
Cdb.products.find({ $and: [ { price: { $lt: 100 } }, { category: { $in: ['electronics', 'appliances'] } } ] })
Ddb.products.find({ price: { $gt: 100 }, category: { $in: ['electronics', 'appliances'] } })
Attempts:
2 left
💡 Hint
Use $and to combine conditions that must both be true.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this MongoDB query
Which option contains a syntax error in combining logical and comparison operators to find users aged between 18 and 30 inclusive?
MongoDB
db.users.find({ age: { $gte: 18, $lte: 30 } })
Adb.users.find({ age: { $gte: 18, $lte: 30 } })
Bdb.users.find({ $and: [ { age: { $gte: 18 } }, { age: { $lte: 30 } } ] })
Cdb.users.find({ age: { $gte: 18 }, age: { $lte: 30 } })
Ddb.users.find({ age: { $gte: 18, $lt: 30 } })
Attempts:
2 left
💡 Hint
Check if the object keys are repeated in the same level.
optimization
advanced
2:00remaining
Optimize query combining multiple logical operators
Which query is the most efficient to find documents where status is 'active' and either score is above 80 or rank is below 10?
Adb.records.find({ $and: [ { status: 'active' }, { $or: [ { score: { $gt: 80 } }, { rank: { $lt: 10 } } ] } ] })
Bdb.records.find({ status: 'active', $or: [ { score: { $gt: 80 } }, { rank: { $lt: 10 } } ] })
Cdb.records.find({ $or: [ { status: 'active', score: { $gt: 80 } }, { status: 'active', rank: { $lt: 10 } } ] })
Ddb.records.find({ $or: [ { status: 'active' }, { score: { $gt: 80 } }, { rank: { $lt: 10 } } ] })
Attempts:
2 left
💡 Hint
Use implicit $and by combining conditions in one object when possible.
🔧 Debug
advanced
2:00remaining
Debug why this query returns no results
A query is intended to find documents where age is between 20 and 40 and verified is true, but it returns no results. Which option explains the likely cause?
MongoDB
db.users.find({ $and: [ { age: { $gt: 20, $lt: 40 } }, { verified: true } ] })
AThe query should use $gte and $lte to include ages 20 and 40; using $gt and $lt excludes boundary ages.
BThe $and operator is invalid syntax in MongoDB queries, causing no results.
CThe query uses $gt and $lt, so it excludes age 20 and 40; if no documents have age strictly between 20 and 40, no results appear.
DThe verified field should be a string 'true' not boolean true, so no matches occur.
Attempts:
2 left
💡 Hint
Check if the comparison operators exclude boundary values.
🧠 Conceptual
expert
3:00remaining
Understanding precedence of logical operators in MongoDB queries
Consider the query: db.collection.find({ $or: [ { status: 'A' }, { $and: [ { qty: { $lt: 30 } }, { price: { $gt: 20 } } ] } ] }). Which statement best describes how MongoDB evaluates this query?
AMongoDB returns documents where status is 'A' or both qty is less than 30 and price is greater than 20.
BMongoDB returns documents where status is 'A' and either qty is less than 30 or price is greater than 20.
CMongoDB returns documents where status is 'A' and qty is less than 30 and price is greater than 20.
DMongoDB returns documents where status is 'A' or qty is less than 30 or price is greater than 20.
Attempts:
2 left
💡 Hint
Recall that $and has higher precedence inside $or arrays.