The $let operator defines local variables inside an aggregation expression. Compute a value once in vars, then reuse it in in — cleaner than repeating the same sub-expression many times.
01
Local Variables
Name and store values.
02
Syntax
vars + in.
03
$$ Reference
Double dollar variables.
04
Reuse Values
Avoid repetition.
05
Use Cases
Totals, labels, logic.
06
Nesting
Scoped inner vars.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $let operator binds one or more variables to expressions, then evaluates a final in expression that can reference those variables. For example, you might compute price × quantity once as $$lineTotal, then use it for tax, discount, and final amount fields without duplicating the multiplication.
Think of $let as a small let block inside your pipeline — similar to declaring a variable in JavaScript before using it in a return expression.
💡
Beginner Tip
Inside in, reference variables with double dollar signs: "$$total". A single $ refers to document fields like "$price". This is the same $$ convention used by $filter and $map.
Foundation
📝 Syntax
The $let operator takes a vars object and an in expression:
$$subtotal is referenced three times inside in without repeating { $multiply: [ "$price", "$quantity" ] }. This keeps the pipeline readable and avoids calculation drift if the formula changes.
Example 3 — $let Inside $cond
Label orders as “bulk” or “standard” based on line total:
The outer $let defines $$base. The inner $let defines $$rate and can use $$base from the outer scope. Notebook (qty 3) gets a 10% discount: 37.5 × 0.9 = 33.75.
The tax variable references $$subtotal defined earlier in the same vars object. MongoDB evaluates vars in order, so dependencies must be declared before use.
Applications
🚀 Use Cases
Avoid repetition — compute a complex expression once and reuse it in multiple fields.
Readable pipelines — name intermediate values like subtotal or margin instead of nested operator trees.
Conditional logic — pair with $cond or $switch using precomputed variables.
Multi-step calculations — chain tax, discount, and shipping fees in a clear order.
🧠 How $let Works
1
MongoDB evaluates vars
Each variable in vars is computed from field paths or expressions, in declaration order.
Bind
2
Variables become $$ references
Inside in, use $$varName to read the bound values for the current document.
Scope
3
The in expression runs
MongoDB evaluates in and returns its result as the output of the surrounding $let.
Output
=
📊
Cleaner expressions
You get named intermediate values and less duplicated logic in your pipeline.
Wrap Up
Conclusion
The $let operator is a small but powerful tool for writing readable aggregation expressions. It lets you name intermediate results, reuse them across fields, and nest scoped variables for multi-step calculations — without repeating the same sub-expression everywhere.
For beginners, remember the pattern: define values in vars, reference them with $$name in in, and keep variable names short but meaningful. When expressions grow complex, reach for $let before adding more nesting.
Use meaningful variable names like subtotal or margin
Reference variables with $$varName inside in
Declare dependencies in order within vars
Extract repeated sub-expressions into $let
Keep in focused — return an object or value you need
❌ Don’t
Use single $ for local variables (use $$)
Reference a variable before it is defined in vars
Over-nest $let when one level with multiple vars suffices
Use $let as a pipeline stage (it is an expression operator)
Confuse $let with JavaScript let outside the pipeline
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $let
Use these points when writing variable expressions in MongoDB.
5
Core concepts
📝01
Local Variables
vars + in pattern.
Purpose
🔢02
$$ Reference
Double dollar vars.
Syntax
🛠03
Reuse Values
Compute once.
Usage
🔄04
$cond Ready
Precompute conditions.
Pattern
📑05
Nestable
Inner scopes OK.
Advanced
❓ Frequently Asked Questions
$let defines local variables inside an aggregation expression and evaluates a final expression that can use those variables. It helps you compute a value once and reuse it, making pipelines easier to read.
The syntax is { $let: { vars: { <name>: <expression>, ... }, in: <expression> } }. Reference variables in the in expression with a double dollar prefix, like "$$total".
Use double dollar signs: if the variable is named total, reference it as "$$total" inside the in expression. A single $ refers to document fields; $$ refers to variables defined in $let or operators like $filter.
Yes. You can place a $let inside another $let's in expression to create scoped variables. Inner variables can shadow outer ones with the same name.
Use $let inside expression stages such as $project, $addFields, $set, and inside other expressions like $cond, $switch, and $map.