MongoDB $or Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $or?

$or means logical OR. A document matches when at least one condition in the array is true. In aggregation expressions it returns true / false the same way.

Remember
$or = any condition true → match / true

Query / $match:
  {
    $or: [
      { price: { $lt: 10 } },
      { inStock: false }
    ]
  }

Aggregation expression:
  {
    $or: [
      { $gt: [ "$price", 20 ] },
      { $lt: [ "$price", 10 ] }
    ]
  }

empty array → false (expression form)
prefer $in when OR-ing equalities on ONE field
NeedPrefer
Any of several conditions$or
All conditions true$and
Same field, many equal values$in

How to Use $or in MongoDB

List conditions in an array. Keep the document (or return true) if any one passes.

  1. For filtering: use find() or $match with { $or: [ {...}, {...} ] }.
  2. For a boolean: use $addFields / $project with expression-style comparisons inside $or.
  3. Put each clause in its own object (query form) or as a boolean expression (agg form).
  4. If every clause is equality on the same field, switch to $in.
PieceRequired?Role
Array of conditionsYesAt least one must be true
Query clauses—Full filter objects like { price: { $lt: 10 } }
Agg clauses—Boolean expressions like { $gt: ["$price", 20] }

Pseudocode

Pseudocode
// Filter mode
for each document:
    if any condition in $or array is true:
        keep document
    else:
        drop document

// Expression mode
for each document:
    result = (cond1 OR cond2 OR ...)
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while writing OR conditions.

TopicDetail
MeaningLogical OR — any condition true
Syntax{ $or: [ { ... }, { ... } ] }
vs $andAll must be true
vs $inSame field, many equal values → use $in
vs $norNone of the conditions true
IndexesBest when every clause can use an index
Sample checkprice < 10 or out of stock → keep either case

Live Preview

Watch $or keep a product when price < 10 or inStock is false.

Simulates { $or: [ { price: { $lt: 10 } }, { inStock: false } ] }.

Result
item = "Pen"
price < 10 → true
inStock false → false
$or → true (kept)

Worked Walkthrough — Cheap or Out of Stock

Keep products that are under $10 or out of stock.

Calculation
Documents:
  { name: "Pen",  price: 5,  inStock: true }
  { name: "Book", price: 15, inStock: false }
  { name: "Lamp", price: 25, inStock: true }

$or: [ price < 10 , inStock == false ]

Pen:  5 < 10 ✓           → keep
Book: inStock false ✓    → keep
Lamp: neither            → drop

Result: Pen and Book

MongoDB Examples

We use three products. First filter with query $or, then build a boolean flag in aggregation. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine three products: a cheap Pen, an out-of-stock Book, and an in-stock expensive Lamp.

mongosh
db.products.insertMany([
  { _id: 1, name: "Pen",  price: 5,  inStock: true },
  { _id: 2, name: "Book", price: 15, inStock: false },
  { _id: 3, name: "Lamp", price: 25, inStock: true }
]);

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps three documents in products. Run find() to see them:

mongosh
db.products.find()

Read those documents like this:

FieldPenBookLamp
price51525
inStocktruefalsetrue
Picture
$or: cheap (< 10)  OR  out of stock

  Pen  → cheap ✓
  Book → out of stock ✓
  Lamp → neither ✗

How It Works

  1. Insert once. insertMany adds three product documents to products.
  2. Two different fields. Price and stock let us show $or across fields (where $in would not fit).
  3. find() shows all rows. The next example keeps only OR matches.

Example 2 — Filter with $or

Goal: return products that are under $10 or out of stock.

mongosh
db.products.find({
  $or: [
    { price: { $lt: 10 } },
    { inStock: false }
  ]
});

How It Works

  1. Two clauses. First checks price; second checks stock.
  2. Any true is enough. Pen matches the price clause; Book matches the stock clause.
  3. Lamp fails both. It is dropped from the result.

Example 3 — Boolean Expression

Same products, but keep every document and flag whether price is over 20 or under 10.

mongosh
db.products.aggregate([
  {
    $addFields: {
      extremePrice: {
        $or: [
          { $gt: ["$price", 20] },
          { $lt: ["$price", 10] }
        ]
      }
    }
  }
]);

How It Works

  1. Expression form. Inside aggregation, use $gt / $lt array syntax.
  2. OR the booleans. Pen (<10) and Lamp (>20) → true; Book (15) → false.
  3. Nothing is dropped. All three rows stay; only the flag differs.

Edge Cases & Pitfalls

Check these before using $or in production queries.

in

Prefer $in for same-field equals

{ status: { $in: ["a", "b"] } } is clearer than $or with two equality clauses on status.

index

Index every clause when you can

MongoDB uses indexes for $or best when all clauses are index-supported; otherwise it may fall back to a collection scan.

syntax

Query vs expression shapes differ

Query: { price: { $lt: 10 } }. Aggregation: { $lt: ["$price", 10] }. Do not mix the two forms.

empty

Empty $or array is false

In aggregation, { $or: [] } returns false. Always pass at least one meaningful clause.

Performance Notes

ApproachCostBest for
Indexed $orIndex scans per clauseWhen every clause has an index
Unindexed $orCollection scanSmall collections / rare queries
Same-field equalsUsually better as $inStatus / type lists

Put selective $or filters in early $match stages, and keep clauses index-friendly when the collection is large.

Key Takeaways

  • Role: logical OR — any condition true.
  • Syntax: { $or: [ {...}, {...} ] }.
  • Works in: find, $match, and expression stages.
  • Tip: same-field equals → prefer $in.

One line: $or answers “is any of these conditions true?”—as a filter or as a boolean.

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>, ... ] }. In find()/$match each element is a query condition. In $project/$addFields each element is a boolean expression.
Use $or in find() filters, $match stages, and aggregation expressions such as $project and $addFields. It works in both query and expression contexts.
$in checks one field against a list of values: { status: { $in: ["active", "pending"] } }. Prefer $or when conditions use different fields or operators.
$or matches when any condition is true. $and matches when every condition is true.
$or matches when at least one condition is true. $nor matches when none of the conditions are true.

Next: $pow

Raise a number to a power in aggregation expressions.

$pow tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful