Challenge - 5 Problems
MongoDB $or Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Understanding $or with multiple conditions
Given a collection
What documents will be returned by this query?
products with documents:{ "name": "Pen", "price": 5, "stock": 100 }{ "name": "Notebook", "price": 15, "stock": 0 }{ "name": "Eraser", "price": 3, "stock": 50 }What documents will be returned by this query?
{ $or: [ { price: { $lt: 5 } }, { stock: { $eq: 0 } } ] }Attempts:
2 left
💡 Hint
Remember $or returns documents matching at least one condition.
✗ Incorrect
The query matches documents where price is less than 5 OR stock equals 0. "Eraser" has price 3 (<5), "Notebook" has stock 0, "Pen" matches neither.
🧠 Conceptual
intermediate1:30remaining
Behavior of $or with empty array
What happens if you run a MongoDB query with
{ $or: [] }?Attempts:
2 left
💡 Hint
Think about what it means to match any condition when there are none.
✗ Incorrect
An empty $or means no conditions to satisfy, so no documents match. It returns zero documents.
📝 Syntax
advanced2:00remaining
Identify the invalid $or query syntax
Which of the following MongoDB queries using $or is syntactically invalid?
Attempts:
2 left
💡 Hint
$or expects an array of conditions, not an object.
✗ Incorrect
Option A uses an object instead of an array for $or, which is invalid syntax.
❓ optimization
advanced2:30remaining
Optimizing $or queries with indexes
You have a collection with indexes on
status and priority. Which $or query will best use these indexes?Attempts:
2 left
💡 Hint
Indexes help when the fields in the query match the indexed fields.
✗ Incorrect
Option A uses $or with conditions on indexed fields status and priority, allowing index use for both parts.
🔧 Debug
expert3:00remaining
Why does this $or query return unexpected results?
Consider this query on a collection of users:
It returns all users, including those with age 22. Why?
{ $or: [ { age: { $gt: 25 } }, { age: { $lt: 20 } } ] }It returns all users, including those with age 22. Why?
Attempts:
2 left
💡 Hint
Check the data type of the age field in the documents.
✗ Incorrect
If age is stored as a string, comparisons like $gt and $lt behave lexicographically, causing unexpected matches like '22' being treated differently.