MongoDB $or Operator

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 4 Examples
Logical

What You’ll Learn

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.

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.

📝 Syntax

The $or operator takes an array of two or more expressions:

mongosh
{ $or: [ <expression1>, <expression2>, ... ] }

Query Example (in find or $match)

mongosh
{ $or: [
  { status: "active" },
  { status: "pending" }
] }

Aggregation Expression Example (in $project)

mongosh
{ $or: [
  { $gte: [ "$age", 65 ] },
  { $eq: [ "$vip", true ] }
] }

Syntax Rules

  • Array required — wrap each condition as a separate object in the array.
  • At least one match — if any condition is true, the document passes.
  • Works in find(), $match, and aggregation expressions.
  • In $match, use field-based conditions directly inside $or.
  • For field-to-field comparisons, wrap with $expr and expression operators.
  • Combine with $and for mixed AND/OR logic.

💡 $or vs $and vs $in vs $nor

$or — at least one condition must match (include if A OR B)
$and — every condition must match (include if A AND B)
$in — one field in a value list: { status: { $in: ["a", "b"] } }
$nor — no condition may match (opposite of $or)

⚡ Quick Reference

QuestionAnswer
Operator typeLogical operator (query + aggregation expression)
Syntax{ $or: [ cond1, cond2, ... ] }
Match ruleAt least one condition must match
Common usagefind(), $match, $project
Logical opposite$nor (none may match)
Same field
{
  $or: [
    { status: "active" },
    { status: "pending" }
  ]
}

Multiple values

Multi-field
{
  $or: [
    { price: { $lt: 10 } },
    { featured: true }
  ]
}

Cross-field OR

In $match
$match: {
  $or: [ ... ]
}

Pipeline filter

$in shortcut
{
  status: {
    $in: ["active","pending"]
  }
}

Same-field alternative

Examples Gallery

Find users by status, filter sale or featured products, use $or in aggregation pipelines, and compare with $in.

📚 User Status Filter

Find users who are active or pending using $or.

Sample Input Documents

Suppose you have a users collection:

mongosh
[
  { "_id": 1, "name": "Alice", "status": "active" },
  { "_id": 2, "name": "Bob",   "status": "pending" },
  { "_id": 3, "name": "Carol", "status": "inactive" },
  { "_id": 4, "name": "Dave",  "status": "active" }
]

Example 1 — $or in find()

Return users who are active or pending:

mongosh
db.users.find({
  $or: [
    { status: "active" },
    { status: "pending" }
  ]
})

How It Works

  • Alice — status is active → included.
  • Bob — status is pending → included.
  • Carol — status is inactive → excluded.
  • Dave — status is active → included.

📈 Practical Patterns

Cross-field conditions, aggregation $match, and when to prefer $in.

Example 2 — Multi-Field OR (Sale or Featured)

Find products that are on sale (price under $10) or marked as featured:

mongosh
db.products.find({
  $or: [
    { price: { $lt: 10 } },
    { featured: true }
  ]
})

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:

mongosh
db.orders.aggregate([
  {
    $match: {
      $or: [
        { status: "completed" },
        { status: "shipped" }
      ]
    }
  },
  {
    $group: {
      _id: "$customerId",
      orderCount: { $sum: 1 },
      revenue: { $sum: "$total" }
    }
  }
])

How It Works

$or in $match narrows the pipeline early. Only completed or shipped orders flow into the $group stage.

Example 4 — $or Inside $expr

Compare field values with expression operators:

mongosh
db.users.find({
  $expr: {
    $or: [
      { $gte: [ "$age", 65 ] },
      { $eq: [ "$vip", true ] }
    ]
  }
})

// Seniors OR VIP members

How It Works

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.

🚀 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.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about $or

Use these points when building flexible filters in MongoDB.

5
Core concepts
📝 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.

Continue the Operator Series

Move on to $pow for exponentiation, or review $and for logical AND.

Next: $pow →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful