The $or operator combines multiple conditions with logical OR. A document matches when at least one condition is true. Use it in find(), $match, and aggregation expressions for flexible filtering.
01
Logical OR
Any condition may match.
02
Array Syntax
Pass conditions in an array.
03
$match Filters
Filter pipeline documents.
04
$expr Mode
Compare field values.
05
Use Cases
Search, status, sales.
06
vs $and
OR vs AND logic.
Fundamentals
Definition and Usage
In MongoDB, the $or operator performs a logical OR on an array of expressions. In a query, a document matches when at least one condition inside $or is true. In an aggregation expression, $or returns true when any sub-expression evaluates to true.
This is especially useful when acceptable values span different fields or use different operators. For example, include products that are on sale (low price) or marked as featured.
💡
Beginner Tip
When all alternatives are values on the same field, $in is often shorter: { status: { $in: ["active", "pending"] } }. Use $or when conditions involve different fields or operators.
Foundation
📝 Syntax
The $or operator takes an array of two or more expressions:
// Widget: price 25, featured false → excluded
// Cheap: price 5, featured false → included (on sale)
// Premium: price 50, featured true → included (featured)
// Bundle: price 8, featured true → included (both match)
How It Works
Only one condition needs to be true. This pattern shines when OR spans different fields — something $in cannot do.
Example 3 — $or in an Aggregation $match Stage
Filter orders that are completed or shipped before grouping revenue:
Inside $expr, use aggregation expression syntax with field paths in arrays. This is useful when conditions compare document fields rather than literal values.
Bonus — When to Use $in Instead
When all alternatives are values on the same field, $in is clearer:
mongosh
// These are equivalent for one field:
db.users.find({
status: { $in: [ "active", "pending" ] }
})
db.users.find({
$or: [
{ status: "active" },
{ status: "pending" }
]
})
How It Works
Prefer $in for same-field value lists. Reserve $or when conditions involve different fields, operators, or complex logic.
Bonus — Combining $and and $or
Mix AND and OR for precise filters:
mongosh
db.products.find({
$and: [
{ inStock: true },
{
$or: [
{ price: { $lt: 20 } },
{ featured: true }
]
}
]
})
// In stock AND (on sale OR featured)
How It Works
Wrap the $or block inside $and to require all top-level conditions while allowing alternatives within the nested $or.
Applications
🚀 Use Cases
Status filtering — include documents with any of several allowed statuses.
Search alternatives — match by name, email, or username in one query.
Sales and promotions — find items on sale or featured across different fields.
Eligibility rules — qualify users who meet any of several criteria (age, VIP, region).
🧠 How $or Works
1
MongoDB evaluates each condition
Every expression in the $or array is tested against each document.
Input
2
$or checks for any match
If at least one condition is true, the document passes. All conditions can be false and the document is excluded.
Logic
3
Matching documents continue
In find() or $match, documents with at least one true condition are returned.
Output
=
✅
Flexible inclusion filters
Documents matching any allowed condition are included in results.
Wrap Up
Conclusion
The $or operator is MongoDB’s logical OR for queries and aggregation expressions. Use it when a document should match if any of several conditions are true, especially across different fields.
For same-field value lists, consider $in first. Pair $or with $and for complex filters. Next in the series: $pow.
Use $or for cross-field or mixed-operator conditions
Put each condition in its own object inside the array
Prefer $in when filtering one field against multiple values
Combine with $and for precise AND/OR logic
Place $match with $or early in pipelines
❌ Don’t
Use $or when $in on one field is simpler
Confuse $or with $nor (none may match)
Forget that only one true condition is enough to include a document
Nest too many levels without parentheses-style grouping via $and
Assume implicit OR exists — comma-separated fields mean AND, not OR
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $or
Use these points when building flexible filters in MongoDB.
5
Core concepts
✅01
Logical OR
Any may match.
Purpose
📝02
Array Syntax
{ $or: [...] }
Syntax
🔍03
Query + Expr
find(), $match, $project.
Usage
🔄04
Opposite of $nor
Include vs exclude.
Logic
📑05
Use $in
Same-field lists.
Alternative
❓ Frequently Asked Questions
$or performs a logical OR on an array of expressions. A document matches when at least one condition is true. In aggregation expressions, $or returns true if any sub-expression evaluates to true.
The syntax is { $or: [ <expression1>, <expression2>, ... ] }. Each element is a query condition in find()/$match, or a boolean expression inside $project or $expr.
Use $or in find() filters, $match stages, and aggregation expressions ($project, $addFields, $cond). It works in both query and expression contexts, unlike $nor which is query-only.
$in checks one field against a list of values: { status: { $in: ["active", "pending"] } }. $or is better when conditions span different fields or use different operators: { $or: [ { price: { $lt: 10 } }, { inStock: false } ] }.
$or matches when at least one condition is true. $nor matches when none of the conditions are true. They are logical opposites for the same condition set.