0
0
MongoDBquery~20 mins

$or operator behavior in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $or Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Understanding $or with multiple conditions
Given a collection 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 } } ] }
ADocuments with name "Pen" and "Eraser"
BDocuments with name "Pen" and "Notebook"
CDocuments with name "Notebook" and "Eraser"
DAll three documents
Attempts:
2 left
💡 Hint
Remember $or returns documents matching at least one condition.
🧠 Conceptual
intermediate
1:30remaining
Behavior of $or with empty array
What happens if you run a MongoDB query with { $or: [] }?
AReturns all documents in the collection
BReturns no documents
CThrows a syntax error
DReturns documents matching all conditions
Attempts:
2 left
💡 Hint
Think about what it means to match any condition when there are none.
📝 Syntax
advanced
2:00remaining
Identify the invalid $or query syntax
Which of the following MongoDB queries using $or is syntactically invalid?
A{ $or: { age: { $lt: 20 }, city: "NY" } }
B{ $or: [ { age: { $gt: 30 } }, { name: "Alice" } ] }
C{ $or: [ { score: { $gte: 90 } }, { grade: "A" } ] }
D{ $or: [ { active: true }, { verified: false } ] }
Attempts:
2 left
💡 Hint
$or expects an array of conditions, not an object.
optimization
advanced
2:30remaining
Optimizing $or queries with indexes
You have a collection with indexes on status and priority. Which $or query will best use these indexes?
A{ $or: [ { status: "open" }, { priority: { $gt: 5 } } ] }
B{ $or: [ { status: "open", priority: { $gt: 5 } } ] }
C{ $or: [ { status: "open" }, { category: "urgent" } ] }
D{ $or: [ { category: "urgent" }, { priority: { $gt: 5 } } ] }
Attempts:
2 left
💡 Hint
Indexes help when the fields in the query match the indexed fields.
🔧 Debug
expert
3:00remaining
Why does this $or query return unexpected results?
Consider this query on a collection of users:
{ $or: [ { age: { $gt: 25 } }, { age: { $lt: 20 } } ] }

It returns all users, including those with age 22. Why?
ABecause the $or conditions cover all ages except 20 to 25, so 22 is excluded
BBecause the query matches users with age greater than 25 OR less than 20, so age 22 does not match and should not be returned
CBecause the query matches users with age greater than 25 OR less than 20, so age 22 does not match either condition
DBecause the query matches users with age greater than 25 OR less than 20, but the field age is stored as a string causing unexpected matches
Attempts:
2 left
💡 Hint
Check the data type of the age field in the documents.