- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Conditional Aggregation Operators >
- $cond (aggregation)
$cond (aggregation)¶
On this page
Definition¶
-
$cond¶ Evaluates a boolean expression to return one of the two specified return expressions.
The
$condexpression has one of two syntaxes:New in version 2.6.
Or:
$condrequires all three arguments (if-then-else) for either syntax.If the
<boolean-expression>evaluates totrue, then$condevaluates and returns the value of the<true-case>expression. Otherwise,$condevaluates and returns the value of the<false-case>expression.The arguments can be any valid expression. For more information on expressions, see Expressions.
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: