- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $cond (aggregation)
$cond (aggregation)¶
On this page
Definition¶
-
$cond
¶ Evaluates a boolean expression to return one of the two specified return expressions.
The
$cond
expression has one of two syntaxes:Or:
$cond
requires all three arguments (if-then-else
) for either syntax.If the
<boolean-expression>
evaluates totrue
, then$cond
evaluates and returns the value of the<true-case>
expression. Otherwise,$cond
evaluates and returns the value of the<false-case>
expression.The arguments can be any valid expression. For more information on expressions, see Expressions.
See also
Example¶
The following example use a inventory
collection with the following
documents:
The following aggregation operation uses the $cond
expression to set the discount
value to 30
if qty
value is
greater than or equal to 250 and to 20
if qty
value is less
than 250
:
The operation returns the following results:
The following operation uses the array syntax of the
$cond
expression and returns the same results: