Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /

$or (query predicate operator)

$or

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

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

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB

  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB

The $or operator has the following syntax:

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

The examples on this page use data from the sample_mflix sample dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the sample dataset. 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:

db.movies.find(
{ $or: [ { runtime: { $gt: 1000 } }, { year: { $lt: 1910 } } ] },
{ _id: 0, title: 1, year: 1, runtime: 1 }
)
[
{
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.

When evaluating the clauses in the $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 expression, all the clauses in the $or expression must be supported by indexes. Otherwise, MongoDB performs a collection scan.

When using indexes with $or queries, each clause of an $or can use its own index. Consider this query:

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:

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

If $or includes a $text query, all clauses in the $or array must be supported by an index. This is because a $text query must use an index, and $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.

$or supports geospatial clauses. However, if you use a near clause ($near or $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):

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" }
]
} )
[
{
_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
]
}
}
}
]

When executing $or queries with a sort(), MongoDB can use indexes that support the $or clauses.

You can create partial indexes with $or. Use the partialFilterExpression of the db.collection.createIndex() method to create a partial index.

If you use $or with <expressions> that are equality checks for the value of the same field, use $in instead of $or.

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

db.movies.find( { year: { $in: [1903, 1909] } },
{ _id: 0, title: 1, year: 1 }
)
[
{ title: 'The Great Train Robbery', year: 1903 },
{ title: 'A Corner in Wheat', year: 1909 }
]

You can nest $or operations.

Tip

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.

Back

$not

On this page