$let - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$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:

Node.js
const { MongoClient } = require('mongodb'); async function calculateRectangleAreas() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('shapes'); const result = await shapes.aggregate([ { $project: { name: 1, area: { $let: { vars: { length: '$length', width: '$width' }, in: { $multiply: ['$$length', '$$width'] } } } } } ]).toArray(); console.log(result); client.close(); } calculateRectangleAreas();
Python
from pymongo import MongoClient def calculate_rectangle_areas(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test collection = db.shapes result = list(shapes.aggregate([ { '$project': { 'name': 1, 'area': { '$let': { 'vars': { 'length': '$length', 'width': '$width' }, 'in': { '$multiply': ['$$length', '$$width'] } } } } } ])) print(result) client.close() calculate_rectangle_areas()