- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $or (aggregation)
$or (aggregation)¶
On this page
Definition¶
-
$or
¶ Evaluates one or more expressions and returns
true
if any of the expressions aretrue
. Otherwise,$or
returnsfalse
.$or
has the following syntax:For more information on expressions, see Expressions.
Behavior¶
In addition to the false
boolean value, $or
evaluates
as false
the following: null
, 0
, and undefined
values. The $or
evaluates all other values as true
,
including non-zero numeric values and arrays.
Example | Result |
---|---|
{ $or: [ true, false ] } |
true |
{ $or: [ [ false ], false ] } |
true |
{ $or: [ null, 0, undefined ] } |
false |
{ $or: [ ] } |
false |
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
:
The following query, which contains multiple expressions supplied to
$or
, may produce an error if there is any document where $x
is 0
: