- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $and (aggregation)
$and (aggregation)¶
On this page
Definition¶
-
$and¶ Evaluates one or more expressions and returns
trueif all of the expressions aretrueor if run with no argument expressions. Otherwise,$andreturnsfalse.$andsyntax:For more information on expressions, see Expressions.
Behavior¶
In addition to the false boolean value, $and evaluates
as false the following: null, 0, and undefined
values. The $and evaluates all other values as true,
including non-zero numeric values and arrays.
| Example | Result |
|---|---|
{ $and: [ 1, "green" ] } |
true |
{ $and: [ ] } |
true |
{ $and: [ [ null ], [ false ], [ 0 ] ] } |
true |
{ $and: [ null, true ] } |
false |
{ $and: [ 0, true ] } |
false |
Error Handling¶
To allow the query engine to optimize queries, $and handles
errors as follows:
- If any expression supplied to
$andwould cause an error when evaluated alone, the$andcontaining the expression may cause an error but an error is not guaranteed. - An expression supplied after the first expression supplied to
$andmay cause an error even if the first expression evaluates tofalse.
For example, the following query always produces an error if $x is
0:
The following query, which contains multiple expressions supplied to
$and, may produce an error if there is any document where $x
is 0: