$or
On this page
$or
-
The
$or
operator performs a logicalOR
operation on an array of one or more<expressions>
and selects the documents that satisfy at least one of the<expressions>
. The$or
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 thequantity
field value is less than20
or theprice
field value equals10
.
Behaviors
$or
Clauses and Indexes
When evaluating the clauses in the $or
expression, MongoDB either performs a collection scan or, if all the clauses are supported by indexes, MongoDB performs index scans. That is, for MongoDB to use indexes to evaluate an $or
expression, all the clauses in the $or
expression must be supported by indexes. Otherwise, MongoDB will perform a collection scan.
When using indexes with $or
queries, each clause of an $or
can use its own index. Consider the following query:
db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )
To support this query, rather than a compound index, you would create one index on quantity
and another index on price
:
db.inventory.createIndex( { quantity: 1 } ) db.inventory.createIndex( { price: 1 } )
MongoDB can use all but the geoHaystack index to support $or
clauses.
$or
and text
Queries
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 will return an error.
$or
and GeoSpatial Queries
$or
supports geospatial clauses with the following exception for the near clause (near clause includes $nearSphere
and $near
). $or
cannot contain a near clause with any other clause.
$or
and Sort Operations
When executing $or
queries with a sort()
, MongoDB can use indexes that support the $or
clauses.
$or
and Partial Indexes
You can create partial indexes with $or
. Use the partialFilterExpression
of the db.collection.createIndex() method to create a partial index.
$or
versus $in
When using $or
with <expressions>
that are equality checks for the value of the same field, use the $in
operator instead of the $or
operator.
For example, to select all documents in the inventory
collection where the quantity
field value equals either 20
or 50
, use the $in
operator:
db.inventory.find ( { quantity: { $in: [20, 50] } } )
Nested $or
Clauses
You may nest $or
operations.
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 totrue
.
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 ] } } ] } )