0
0
MongoDBquery~10 mins

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

Choose your learning style9 modes available
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.