Docs Menu
Docs Home
/ /

$or

$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> } ] }

Consider the following example:

db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )

This query will select all documents in the inventory collection where either the quantity field value is less than 20 or the price field value equals 10.

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.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )

To support this query, rather than a compound index, create one index on quantity and another index on price:

db.inventory.createIndex( { quantity: 1 } )
db.inventory.createIndex( { price: 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.

For example, the following query is invalid because the $or operator combines $near with another clause:

db.places.find( {
$or: [
{ location: { $near: [ 40, 5 ] } },
{ category: "restaurant" }
]
} )

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

db.places.find( {
$or: [
{
location: {
$geoIntersects: {
$geometry: {
type: "Polygon",
coordinates: [
[ [ 39, 4 ], [ 41, 4 ], [ 41, 6 ], [ 39, 4 ] ]
]
}
}
}
},
{ category: "restaurant" }
]
} )

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 inventory collection where quantity is 20 or 50:

db.inventory.find ( { quantity: { $in: [20, 50] } } )

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.

For example, the following query always produces an error if $x is 0:

db.example.find( {
$expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] }
} )

The following query, which contains multiple expressions supplied to $or, may produce an error if there is any document where $x is 0:

db.example.find( {
$or: [
{ x: { $eq: 0 } },
{ $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } }
]
} )

Back

$not

On this page