$let
The $let operator in Amazon DocumentDB is used to bind variables to values and use those variables in the expression. It allows you to define local variables that can be used in subsequent expressions within the same stage of the aggregation pipeline.
Parameters
-
vars: An object that defines the variables to be used in the expression. -
in: The expression in which the variables defined in the vars parameter are used.
Example (MongoDB Shell)
This example demonstrates the usage of the $let operator to calculate the area of a rectangle.
Create sample documents
db.shapes.insertMany([ { name: "Rectangle 1", length: 5, width: 3 }, { name: "Rectangle 2", length: 7, width: 4 }, { name: "Rectangle 3", length: 6, width: 2 } ]);
Query example
db.shapes.aggregate([ { $project: { name: 1, area: { $let: { vars: { length: "$length", width: "$width" }, in: { $multiply: ["$$length", "$$width"] } } } } } ])
Output
[
{
"_id": ObjectId("6161e5b1a3eba3c7f2960d03"),
"name": "Rectangle 1",
"area": 15
},
{
"_id": ObjectId("6161e5b1a3eba3c7f2960d04"),
"name": "Rectangle 2",
"area": 28
},
{
"_id": ObjectId("6161e5b1a3eba3c7f2960d05"),
"name": "Rectangle 3",
"area": 12
}
]
Code examples
To view a code example for using the $let command, choose the tab for the language that you want to use: