Bird
Raised Fist0
MongoDBquery~10 mins

$or operator behavior in MongoDB - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - $or operator behavior
Start Query
Evaluate each condition inside $or
Is any condition TRUE?
NoReturn no documents
Yes
Return documents matching any condition
The $or operator checks multiple conditions and returns documents if at least one condition is true.
Execution Sample
MongoDB
db.collection.find({
  $or: [
    { age: { $lt: 30 } },
    { city: 'New York' }
  ]
})
Find documents where age is less than 30 OR city is New York.
Execution Table
StepDocumentCondition 1 (age < 30)Condition 2 (city = 'New York')Result (Match?)
1{ name: 'Alice', age: 25, city: 'Boston' }True (25 < 30)False (Boston != New York)Match (True OR False = True)
2{ name: 'Bob', age: 35, city: 'New York' }False (35 < 30)True (New York = New York)Match (False OR True = True)
3{ name: 'Carol', age: 40, city: 'Chicago' }False (40 < 30)False (Chicago != New York)No Match (False OR False = False)
💡 All documents evaluated; only those with at least one true condition are returned.
Variable Tracker
VariableStartAfter Doc 1After Doc 2After Doc 3
Condition 1 (age < 30)N/ATrueFalseFalse
Condition 2 (city = 'New York')N/AFalseTrueFalse
Match ResultN/ATrueTrueFalse
Key Moments - 2 Insights
Why does the document with age 25 and city Boston match even though city is not New York?
Because $or returns true if any condition is true. Here, age < 30 is true (see execution_table row 1), so the document matches.
What happens if none of the conditions inside $or are true for a document?
The document does not match and is not returned. See execution_table row 3 where both conditions are false.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the match result for the document { name: 'Bob', age: 35, city: 'New York' }?
ANo Match
BMatch
CError
DUnknown
💡 Hint
Check execution_table row 2 under 'Result (Match?)'
At which document does the $or condition evaluate to false?
ADocument 1
BDocument 2
CDocument 3
DNone
💡 Hint
Look at execution_table row 3 where both conditions are false
If we change condition 1 to age < 20, which document(s) would match?
AOnly document 2
BDocuments 1 and 2
COnly document 1
DNo documents
💡 Hint
Refer to variable_tracker for condition 1 values and execution_table results
Concept Snapshot
$or operator in MongoDB:
- Takes an array of conditions.
- Returns documents matching at least one condition.
- Works like logical OR in everyday decisions.
- Useful to find documents meeting any of multiple criteria.
- Syntax: { $or: [condition1, condition2, ...] }
Full Transcript
The $or operator in MongoDB checks multiple conditions for each document. If any condition is true, the document is returned. For example, a query with $or: [{age: {$lt: 30}}, {city: 'New York'}] returns documents where age is less than 30 or city is New York. The execution table shows step-by-step evaluation for each document. Documents with at least one true condition match. This behavior is like everyday OR logic: if any condition is true, the whole is true. Beginners often wonder why a document matches if only one condition is true; $or requires only one true condition to match. If none are true, the document is excluded. Changing conditions affects which documents match, as shown in the quiz. This visual trace helps understand $or's behavior clearly.

Practice

(1/5)
1.

What does the $or operator do in MongoDB queries?

easy
A. It returns documents that match at least one of the given conditions.
B. It returns documents that match all given conditions simultaneously.
C. It returns documents that do not match any of the given conditions.
D. It sorts documents based on multiple fields.

Solution

  1. Step 1: Understand the purpose of $or

    The $or operator is used to find documents that satisfy at least one condition from multiple conditions.
  2. Step 2: Compare with other operators

    Unlike $and, which requires all conditions to be true, $or requires only one condition to be true.
  3. Final Answer:

    It returns documents that match at least one of the given conditions. -> Option A
  4. Quick Check:

    $or = match any condition [OK]
Hint: Think 'or' means any one condition true [OK]
Common Mistakes:
  • Confusing $or with $and operator
  • Thinking $or filters documents matching all conditions
  • Assuming $or sorts documents
2.

Which of the following is the correct syntax to use $or in a MongoDB query?

{ $or: [ { age: { $lt: 20 } }, { city: "NY" } ] }
easy
A. { $or: { age: { $lt: 20 }, city: "NY" } }
B. { $or: [ age: { $lt: 20 }, city: "NY" ] }
C. { $or: [ { age: { $lt: 20 } }, { city: "NY" } ] }
D. { $or: ( age: { $lt: 20 }, city: "NY" ) }

Solution

  1. Step 1: Check the structure of $or

    The $or operator requires an array of condition objects inside square brackets.
  2. 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.
  3. Final Answer:

    { $or: [ { age: { $lt: 20 } }, { city: "NY" } ] } -> Option C
  4. Quick Check:

    $or needs array of conditions [OK]
Hint: Remember: $or takes an array of condition objects [OK]
Common Mistakes:
  • Using curly braces instead of square brackets for conditions
  • Using parentheses instead of brackets
  • Not wrapping conditions inside an array
3.

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?

medium
A. [{ "name": "Bob", "age": 19, "city": "LA" }, { "name": "Carol", "age": 30, "city": "SF" }]
B. [{ "name": "Alice", "age": 25, "city": "NY" }]
C. [{ "name": "Bob", "age": 19, "city": "LA" }]
D. []

Solution

  1. Step 1: Identify documents matching each condition

    Condition 1: age < 20 matches Bob (age 19). Condition 2: city = "SF" matches Carol.
  2. Step 2: Combine results with $or

    The query returns documents matching either condition, so Bob and Carol are included.
  3. Final Answer:

    [{ "name": "Bob", "age": 19, "city": "LA" }, { "name": "Carol", "age": 30, "city": "SF" }] -> Option A
  4. Quick Check:

    $or returns any matching document [OK]
Hint: Check each condition separately, then combine results [OK]
Common Mistakes:
  • Including documents that don't match any condition
  • Confusing $or with $and and expecting all conditions to match
  • Ignoring one of the conditions
4.

Consider this query that causes an error:

db.collection.find({ $or: { age: { $gt: 30 }, city: "NY" } })

What is the main issue causing the error?

medium
A. The field names inside $or must be strings.
B. The query is missing a closing parenthesis.
C. The operator $gt cannot be used inside $or.
D. $or requires an array of conditions, not a single object.

Solution

  1. Step 1: Analyze the $or syntax

    $or expects an array of condition objects, but here it is given a single object.
  2. Step 2: Identify the error cause

    Because the value is not an array, MongoDB throws a syntax error.
  3. Final Answer:

    $or requires an array of conditions, not a single object. -> Option D
  4. Quick Check:

    $or needs array syntax [OK]
Hint: Always wrap $or conditions in square brackets [OK]
Common Mistakes:
  • Using object instead of array for $or conditions
  • Misplacing operators inside $or
  • Ignoring syntax errors from missing brackets
5.

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?

hard
A. db.products.find({ category: "electronics" && price: { $lt: 100 } })
B. db.products.find({ $or: [ { category: "electronics" }, { price: { $lt: 100 } } ] })
C. db.products.find({ $or: { category: "electronics", price: { $lt: 100 } } })
D. db.products.find({ $and: [ { category: "electronics" }, { price: { $lt: 100 } } ] })

Solution

  1. Step 1: Understand the query goal

    We want documents where category is "electronics" OR price is less than 100.
  2. Step 2: Check each option's logic and syntax

    db.products.find({ $or: [ { category: "electronics" }, { price: { $lt: 100 } } ] }) correctly uses $or with 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.
  3. Final Answer:

    db.products.find({ $or: [ { category: "electronics" }, { price: { $lt: 100 } } ] }) -> Option B
  4. Quick Check:

    $or with array matches either condition [OK]
Hint: Use array inside $or for multiple conditions [OK]
Common Mistakes:
  • Using object instead of array for $or
  • Confusing $or with $and
  • Using invalid logical operators like && in MongoDB queries