0
0
MongoDBquery~20 mins

Combining comparison operators in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Combining Comparison Operators
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents with age between 25 and 35 inclusive
Given a collection users with documents containing an age field, which query returns all users whose age is between 25 and 35 inclusive?
MongoDB
db.users.find({ age: { $gte: 25, $lte: 35 } })
Adb.users.find({ age: { $gte: 25 }, age: { $lte: 35 } })
Bdb.users.find({ age: { $gt: 25, $lt: 35 } })
Cdb.users.find({ age: { $gte: 25, $lte: 35 } })
Ddb.users.find({ age: { $gte: 25 } }, { age: { $lte: 35 } })
Attempts:
2 left
💡 Hint
Use $gte and $lte together inside the same object for the field.
query_result
intermediate
2:00remaining
Find documents where score is less than 50 or greater than 90
Which MongoDB query returns documents where the score field is either less than 50 or greater than 90?
MongoDB
db.scores.find({ $or: [ { score: { $lt: 50 } }, { score: { $gt: 90 } } ] })
Adb.scores.find({ score: { $lt: 50, $gt: 90 } })
Bdb.scores.find({ score: { $lt: 50 } }, { score: { $gt: 90 } })
Cdb.scores.find({ $and: [ { score: { $lt: 50 } }, { score: { $gt: 90 } } ] })
Ddb.scores.find({ $or: [ { score: { $lt: 50 } }, { score: { $gt: 90 } } ] })
Attempts:
2 left
💡 Hint
Use $or to combine conditions that can be true alternatively.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this query combining comparison operators
Which option contains a syntax error when trying to find documents with price between 100 and 200?
MongoDB
db.products.find({ price: { $gte: 100, $lte: 200 } })
Adb.products.find({ price: { $gte: 100, $lte: 200 } })
Bdb.products.find({ price: { $gte: 100 }, price: { $lte: 200 } })
Cdb.products.find({ price: { $gte: 100, $lte: 200 } }, {})
Ddb.products.find({ price: { $gte: 100, $lte: 200 }, })
Attempts:
2 left
💡 Hint
Check if the same field appears twice in the same object.
optimization
advanced
2:00remaining
Optimize query to find documents with age not between 20 and 30
Which query is the most efficient and correct way to find documents where age is NOT between 20 and 30 inclusive?
Adb.users.find({ $or: [ { age: { $lt: 20 } }, { age: { $gt: 30 } } ] })
Bdb.users.find({ age: { $not: { $gte: 20, $lte: 30 } } })
Cdb.users.find({ age: { $ne: { $gte: 20, $lte: 30 } } })
Ddb.users.find({ age: { $lt: 20, $gt: 30 } })
Attempts:
2 left
💡 Hint
Use $or to combine conditions that exclude the range.
🧠 Conceptual
expert
2:00remaining
Understanding behavior of combined comparison operators in MongoDB
Consider the query db.collection.find({ value: { $gt: 10, $lt: 20 } }). Which statement about this query is true?
AIt returns documents where value is greater than 10 and less than 20.
BIt returns documents where value is greater than 10 or less than 20.
CIt returns documents where value is exactly 10 or 20.
DIt causes a syntax error because multiple operators cannot be combined.
Attempts:
2 left
💡 Hint
Think about how MongoDB interprets multiple operators inside one field object.