On this page本页内容
$cond¶Evaluates a boolean expression to return one of the two specified return expressions.计算布尔表达式以返回两个指定的返回表达式之一。
The $cond expression has one of two syntaxes:$cond表达式有两个语法之一:
Or:或者:
$cond requires all three arguments (if-then-else) for either syntax.$cond需要这两种语法的所有三个参数(if-then-else)。
If the 如果<boolean-expression> evaluates to true, then $cond evaluates and returns the value of the <true-case> expression. <boolean-expression>的计算结果为true,则$cond将计算并返回<true-case>表达式的值。Otherwise, 否则,$cond evaluates and returns the value of the <false-case> expression.$cond计算并返回<false-case>表达式的值。
The arguments can be any valid expression. 参数可以是任何有效的表达式。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
See also参阅
The following example use a 以下示例将inventory collection with the following documents:inventory集合与以下文档一起使用:
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:$cond表达式将discount值设置为30(如果qty值大于或等于250),并将discount值设置为20(如果数量值小于250):
The operation returns the following results:操作返回以下结果:
The following operation uses the array syntax of the 以下操作使用$cond expression and returns the same results:$cond表达式的数组语法,并返回相同的结果: