$let (aggregation)

On this page本页内容

Definition定义

$let

Binds variables for use in the specified expression, and returns the result of the expression.绑定变量以在指定表达式中使用,并返回表达式的结果。

The $let expression has the following syntax:let表达式语法如下:

{
  $let:
     {
       vars: { <var1>: <expression>, ... },
       in: <expression>
     }
}
Field字段Specification规格
vars

Assignment block for the variables accessible in the in expression. in表达式中可访问的变量的赋值块。To assign a variable, specify a string for the variable name and assign a valid expression for the value.要指定变量,请为变量名指定字符串,并为值指定有效表达式。

The variable assignments have no meaning outside the in expression, not even within the vars block itself.变量赋值在in表达式外没有意义,甚至在vars块本身内也没有意义。

inThe expression to evaluate.要计算的表达式

To access variables in aggregation expressions, prefix the variable name with double dollar signs ($$) and enclosed in quotes. 要访问聚合表达式中的变量,请在变量名前面加上双美元符号($$)并用引号括起来。For more information on expressions, see Expressions. 有关表达式的详细信息,请参阅表达式For information on use of variables in the aggregation pipeline, see Variables in Aggregation Expressions.有关在聚合管道中使用变量的信息,请参阅聚合表达式中的变量

Behavior行为

$let can access variables defined outside its expression block, including system variables.可以访问在其表达式块外部定义的变量,包括系统变量

If you modify the values of externally defined variables in the vars block, the new values take effect only in the in expression. 如果修改vars块中外部定义变量的值,则新值仅在in表达式中生效。Outside of the in expression, the variables retain their previous values.in表达式之外,变量保留其以前的值。

In the vars assignment block, the order of the assignment does not matter, and the variable assignments only have meaning inside the in expression. vars赋值块中,赋值顺序无关紧要,变量赋值只在in表达式中有意义。As such, accessing a variable's value in the vars assignment block refers to the value of the variable defined outside the vars block and not inside the same vars block.因此,访问vars赋值块中的变量值是指在vars块外而不是在同一vars块内定义的变量值。

For example, consider the following $let expression:例如,考虑以下$let表达式:

{
  $let:
    {
      vars: { low: 1, high: "$$low" },
      in: { $gt: [ "$$low", "$$high" ] }
    }
}

In the vars assignment block, "$$low" refers to the value of an externally defined variable low and not the variable defined in the same vars block. vars赋值块中,"$$low"指的是外部定义的变量low的值,而不是同一vars块中定义的变量。If low is not defined outside this $let expression block, the expression is invalid.如果未在此$let表达式块外定义low,则表达式无效。

Example示例

A sales collection has the following documents:sales集合包含以下文档:

{ _id: 1, price: 10, tax: 0.50, applyDiscount: true }
{ _id: 2, price: 10, tax: 0.25, applyDiscount: false }

The following aggregation uses $let in the $project pipeline stage to calculate and return the finalTotal for each document:以下聚合使用$project管道阶段中的$let计算并返回每个文档的finalTotal

db.sales.aggregate( [
   {
      $project: {
         finalTotal: {
            $let: {
               vars: {
                  total: { $add: [ '$price', '$tax' ] },
                  discounted: { $cond: { if: '$applyDiscount', then: 0.9, else: 1 } }
               },
               in: { $multiply: [ "$$total", "$$discounted" ] }
            }
         }
      }
   }
] )

The aggregation returns the following results:聚合返回以下结果:

{ "_id" : 1, "finalTotal" : 9.450000000000001 }
{ "_id" : 2, "finalTotal" : 10.25 }
Tip提示
See also: 参阅:
←  $lastN (array operator)$linearFill (aggregation) →