OPTIONS

$let (aggregation)

Definition

$let

Binds variables for use in subexpressions. To access the variable in the subexpressions, use a string with the variable name prefixed with double dollar signs ($$).

The $let expression has the following syntax:

{
  $let:
     {
       vars: { <var1>: <value1>, ... },
       in: { <expression using "$$var1", ...> }
     }
}
Returns:The value of the subexpression evaluated with the bound variables.

See Variables in Aggregation for more information on using variables in the aggregation pipeline.

Behavior

In the vars: { <var1>: <value1>, ... } assignment block, the order of the assignment does not matter, and using $$var to access a variable’s value refers to the existing value, if any, of the variable. Even if the variable is being reassigned, $$var would refer to the current and not the reassigned value in the assignment block.

For example, the following $let expression is invalid since in the vars: { low: 1, high: "$$low" } assignment block, "$$low" refers to the pre-assignment value of the variable low, which is undefined:

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

$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 subexpression, and the variables retain to their previous values outside the in subexpression.

Examples

Project Values Calculated Using Variables

A sales collection has the following documents:

{ _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:

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 }

See also

$map