> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $or (query predicate operator)

[`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) performs a logical `OR` operation on an array of one or more expressions and selects documents that satisfy at least one of the expressions.

## Compatibility

You can use `$or` for deployments hosted in the following environments:

- [MongoDB Atlas](https://www.mongodb.com/docs/atlas): The fully managed service for MongoDB deployments in the cloud

* [MongoDB Enterprise](https://www.mongodb.com/docs/manual/administration/install-enterprise.md#std-label-install-mdb-enterprise): The subscription-based, self-managed version of MongoDB

* [MongoDB Community](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition): The source-available, free-to-use, and self-managed version of MongoDB

## Syntax

The [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) operator has the following syntax:

```javascript
{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
```

The examples on this page use data from the [sample\_mflix dataset](https://www.mongodb.com/docs/manual/sample-data/sample-mflix.md#std-label-sample-mflix). For details on how to load this dataset into your self-managed MongoDB deployment, see [Load the Sample Dataset](https://www.mongodb.com/docs/manual/sample-data/load-sample-data-local.md#std-label-sample-dataset-local). If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.

Consider the following example:

```javascript
db.movies.find(
   { $or: [ { runtime: { $gt: 1000 } }, { year: { $lt: 1910 } } ] },
   { _id: 0, title: 1, year: 1, runtime: 1 }
)

```

**Output:**

```javascript
[
  {
    runtime: 11,
    title: 'The Great Train Robbery',
    year: 1903
  },
  {
    runtime: 14,
    title: 'A Corner in Wheat',
    year: 1909
  },
  {
    runtime: 1256,
    title: 'Centennial',
    year: 1978
  },
  {
    runtime: 1140,
    title: 'Baseball',
    year: 1994
  },
  {
    runtime: 1,
    title: 'The Kiss',
    year: 1896
  },
  {
    runtime: 1,
    title: 'The Kiss',
    year: 1896
  }
]

```

This query selects all documents in the `movies` collection that meet either of the following conditions:

- The `runtime` field value is greater than `1000`.

- The `year` field value is earlier than `1910`.

## Behaviors

### `$or` Clauses and Indexes

When evaluating the clauses in the [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) expression, MongoDB performs a collection scan or an index scan. If all clauses are supported by indexes, MongoDB performs index scans. To use indexes to evaluate an [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) expression, all the clauses in the [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) expression must be supported by indexes. Otherwise, MongoDB performs a collection scan.

When using indexes with [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) queries, each clause of an [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) can use its own index. Consider this query:

```javascript
db.movies.find(
   { $or: [ { runtime: { $gt: 1000 } }, { year: { $lt: 1910 } } ] }
)

```

To support this query, create one index on `runtime` and another index on `year`, rather than a compound index:

```javascript
db.movies.createIndex( { runtime: 1 } ),
db.movies.createIndex( { year: 1 } ),

```

### `$or` and `text` Queries

If [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) includes a [`$text`](https://www.mongodb.com/docs/manual/reference/operator/query/text.md#mongodb-query-op.-text) query, all clauses in the [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) array must be supported by an index. This is because a `$text` query *must* use an index, and [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) can only use indexes if all its clauses are supported by indexes. If the `$text` query cannot use an index, the query returns an error.

**Note:**

`$text` provides text query capabilities for self-managed (non-Atlas) deployments. For data hosted on MongoDB, MongoDB also offers an improved full-text query solution, [MongoDB Search.](https://www.mongodb.com/docs/atlas/atlas-search/)

### `$or` and Geospatial Queries

`$or` supports [geospatial clauses](https://www.mongodb.com/docs/manual/reference/mql/query-predicates/geospatial.md#std-label-geospatial-query-operators). However, if you use a near clause ([`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) or [`$nearSphere`](https://www.mongodb.com/docs/manual/reference/operator/query/nearsphere.md#mongodb-query-op.-nearSphere)), `$or` cannot contain any other clauses. Using `$or` with a single clause has the same effect as omitting the `$or` operator.

The following query is valid because `$or` uses a non-near geospatial clause (`$geoIntersects`):

```javascript
db.theaters.find( {
   $or: [
      {
         "location.geo": {
            $geoIntersects: {
               $geometry: {
                  type: "Polygon",
                  coordinates: [
                     [ [ -74.5, 40.5 ], [ -73.5, 40.5 ],
                       [ -73.5, 41.0 ], [ -74.5, 40.5 ] ]
                  ]
               }
            }
         }
      },
      { "location.address.state": "NY" }
   ]
} )
.sort({ theaterId: 1 }).limit(5)

```

**Output:**

```javascript
[
  {
    _id: ObjectId('59a47287cfa9a3a73e51e92f'),
    theaterId: 200,
    location: {
      address: {
        street1: '3124 Jericho Tpke',
        city: 'East Northport',
        state: 'NY',
        zipcode: '11731'
      },
      geo: {
        type: 'Point',
        coordinates: [
          -73.319092,
          40.838463
        ]
      }
    }
  },
  {
    _id: ObjectId('59a47287cfa9a3a73e51ead6'),
    theaterId: 345,
    location: {
      address: {
        street1: '148 Walt Whitman Rd',
        city: 'Huntington Station',
        state: 'NY',
        zipcode: '11746'
      },
      geo: {
        type: 'Point',
        coordinates: [
          -73.410637,
          40.825775
        ]
      }
    }
  },
  {
    _id: ObjectId('59a47287cfa9a3a73e51eae8'),
    theaterId: 374,
    location: {
      address: {
        street1: '2478 Central Park Ave',
        city: 'Yonkers',
        state: 'NY',
        zipcode: '10710'
      },
      geo: {
        type: 'Point',
        coordinates: [
          -73.826805,
          40.983246
        ]
      }
    }
  },
  {
    _id: ObjectId('59a47287cfa9a3a73e51eafd'),
    theaterId: 384,
    location: {
      address: {
        street1: '40 Catherwood Road',
        city: 'Ithaca',
        state: 'NY',
        zipcode: '14850'
      },
      geo: {
        type: 'Point',
        coordinates: [
          -76.492142,
          42.481991
        ]
      }
    }
  },
  {
    _id: ObjectId('59a47287cfa9a3a73e51eb2c'),
    theaterId: 428,
    location: {
      address: {
        street1: '1 Crossgates Mall Rd',
        city: 'Albany',
        state: 'NY',
        zipcode: '12203'
      },
      geo: {
        type: 'Point',
        coordinates: [
          -73.848686,
          42.690285
        ]
      }
    }
  }
]

```

### `$or` and Sort Operations

When executing [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) queries with a [`sort()`](https://www.mongodb.com/docs/manual/reference/method/cursor.sort.md#mongodb-method-cursor.sort), MongoDB can use indexes that support the [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) clauses.

### `$or` and Partial Indexes

You can create [partial indexes](https://www.mongodb.com/docs/manual/core/index-partial.md#std-label-index-type-partial) with [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or). Use the `partialFilterExpression` of the [db.collection.createIndex()](https://www.mongodb.com/docs/manual/reference/method/db.collection.createindex.md#std-label-method-createIndex) method to create a partial index.

### `$or` Compared to `$in`

If you use [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) with `<expressions>` that are equality checks for the value of the same field, use [`$in`](https://www.mongodb.com/docs/manual/reference/operator/query/in.md#mongodb-query-op.-in) instead of [`$or`.](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or)

This query selects documents in the `movies` collection where `year` is `1903` or `1909`:

```javascript
db.movies.find( { year: { $in: [1903, 1909] } },
   { _id: 0, title: 1, year: 1 }
)

```

**Output:**

```javascript
[
  { title: 'The Great Train Robbery', year: 1903 },
  { title: 'A Corner in Wheat', year: 1909 }
]

```

### Nested `$or` Clauses

You can nest [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or) operations.

**See also:**

- [`$and`](https://www.mongodb.com/docs/manual/reference/operator/query/and.md#mongodb-query-op.-and)

- [`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find)

- [`sort()`](https://www.mongodb.com/docs/manual/reference/method/cursor.sort.md#mongodb-method-cursor.sort)

- [`$in`](https://www.mongodb.com/docs/manual/reference/operator/query/in.md#mongodb-query-op.-in)

### Error Handling

To allow the query engine to optimize queries, `$or` handles errors as follows:

- If any expression supplied to `$or` would cause an error when evaluated alone, the `$or` containing the expression may cause an error but an error is not guaranteed.

- An expression supplied after the first expression supplied to `$or` may cause an error even if the first expression evaluates to `true`.
