The $or operator helps find documents that match at least one of several conditions. It makes searching flexible by allowing multiple options.
$or operator behavior in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
db.collection.find({ $or: [ { condition1 }, { condition2 }, ... ] })The $or operator takes an array of conditions inside curly braces.
It returns documents that satisfy at least one of the conditions.
db.users.find({ $or: [ { city: "New York" }, { city: "Los Angeles" } ] })db.products.find({ $or: [ { price: { $lt: 20 } }, { onSale: true } ] })db.orders.find({ $or: [ { status: "pending" }, { status: "shipped" } ] })This example inserts four employees and then finds those who work in HR or live in Chicago.
db.employees.insertMany([
{ name: "Alice", department: "HR", city: "New York" },
{ name: "Bob", department: "IT", city: "Chicago" },
{ name: "Carol", department: "HR", city: "Los Angeles" },
{ name: "Dave", department: "Finance", city: "New York" }
])
db.employees.find({ $or: [ { department: "HR" }, { city: "Chicago" } ] })If no documents match any condition, the result is empty.
Conditions inside $or can be any valid query expressions.
You can combine $or with other operators for complex queries.
$or finds documents matching at least one condition.
Use it to search with multiple options easily.
It takes an array of conditions and returns matching documents.
Practice
What does the $or operator do in MongoDB queries?
Solution
Step 1: Understand the purpose of
The$or$oroperator is used to find documents that satisfy at least one condition from multiple conditions.Step 2: Compare with other operators
Unlike$and, which requires all conditions to be true,$orrequires only one condition to be true.Final Answer:
It returns documents that match at least one of the given conditions. -> Option AQuick Check:
$or= match any condition [OK]
- Confusing $or with $and operator
- Thinking $or filters documents matching all conditions
- Assuming $or sorts documents
Which of the following is the correct syntax to use $or in a MongoDB query?
{ $or: [ { age: { $lt: 20 } }, { city: "NY" } ] }Solution
Step 1: Check the structure of
The$or$oroperator requires an array of condition objects inside square brackets.Step 2: Validate each option's syntax
{ $or: [ { age: { $lt: 20 } }, { city: "NY" } ] } correctly uses an array with two objects. Options A, B, and C use incorrect brackets or missing array syntax.Final Answer:
{ $or: [ { age: { $lt: 20 } }, { city: "NY" } ] } -> Option CQuick Check:
$orneeds array of conditions [OK]
- Using curly braces instead of square brackets for conditions
- Using parentheses instead of brackets
- Not wrapping conditions inside an array
Given the collection users with documents:
[{ "name": "Alice", "age": 25, "city": "NY" }, { "name": "Bob", "age": 19, "city": "LA" }, { "name": "Carol", "age": 30, "city": "SF" }]What will the query db.users.find({ $or: [ { age: { $lt: 20 } }, { city: "SF" } ] }) return?
Solution
Step 1: Identify documents matching each condition
Condition 1: age < 20 matches Bob (age 19). Condition 2: city = "SF" matches Carol.Step 2: Combine results with
The query returns documents matching either condition, so Bob and Carol are included.$orFinal Answer:
[{ "name": "Bob", "age": 19, "city": "LA" }, { "name": "Carol", "age": 30, "city": "SF" }] -> Option AQuick Check:
$orreturns any matching document [OK]
- Including documents that don't match any condition
- Confusing $or with $and and expecting all conditions to match
- Ignoring one of the conditions
Consider this query that causes an error:
db.collection.find({ $or: { age: { $gt: 30 }, city: "NY" } })What is the main issue causing the error?
Solution
Step 1: Analyze the
$orsyntax$orexpects an array of condition objects, but here it is given a single object.Step 2: Identify the error cause
Because the value is not an array, MongoDB throws a syntax error.Final Answer:
$orrequires an array of conditions, not a single object. -> Option DQuick Check:
$orneeds array syntax [OK]
$or conditions in square brackets [OK]- Using object instead of array for
$orconditions - Misplacing operators inside
$or - Ignoring syntax errors from missing brackets
You want to find documents in a products collection where the category is either "electronics" or the price is less than 100. Which query correctly uses $or to achieve this?
Solution
Step 1: Understand the query goal
We want documents where category is "electronics" OR price is less than 100.Step 2: Check each option's logic and syntax
db.products.find({ $or: [ { category: "electronics" }, { price: { $lt: 100 } } ] }) correctly uses$orwith an array of two conditions. db.products.find({ $or: { category: "electronics", price: { $lt: 100 } } }) uses an object instead of array, causing syntax error. db.products.find({ category: "electronics" && price: { $lt: 100 } }) uses invalid syntax with &&. db.products.find({ $and: [ { category: "electronics" }, { price: { $lt: 100 } } ] }) uses$and, which requires both conditions to be true, not either.Final Answer:
db.products.find({ $or: [ { category: "electronics" }, { price: { $lt: 100 } } ] }) -> Option BQuick Check:
$orwith array matches either condition [OK]
$or for multiple conditions [OK]- Using object instead of array for
$or - Confusing
$orwith$and - Using invalid logical operators like && in MongoDB queries
