Skip to content

Commit 42fa97f

Browse files
authored
Merge pull request #2992 from abinav2307/variable-expression-third-pass-validation
Revise $let operator documentation
2 parents f33c092 + e26fb06 commit 42fa97f

File tree

1 file changed

+109
-72
lines changed
  • articles/cosmos-db/mongodb/vcore/operators/variable-expression

1 file changed

+109
-72
lines changed

articles/cosmos-db/mongodb/vcore/operators/variable-expression/$let.md

Lines changed: 109 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
---
2-
title: $let (variable expression) usage on Azure Cosmos DB for MongoDB vCore
3-
titleSuffix: Azure Cosmos DB for MongoDB vCore
2+
title: $let
3+
titleSuffix: Overview of the $let operator in Azure Cosmos DB for MongoDB (vCore)
44
description: The $let operator allows defining variables for use in a specified expression, enabling complex calculations and reducing code repetition.
55
author: suvishodcitus
66
ms.author: suvishod
77
ms.service: azure-cosmos-db
88
ms.subservice: mongodb-vcore
99
ms.topic: language-reference
10-
ms.date: 02/12/2025
10+
ms.date: 09/04/2025
1111
---
1212

13-
# $let (variable expression)
14-
15-
[!INCLUDE[MongoDB (vCore)](~/reusable-content/ce-skilling/azure/includes/cosmos-db/includes/appliesto-mongodb-vcore.md)]
13+
# $let
1614

1715
The `$let` operator is used to define variables for use in a specified expression. It allows you to create temporary variables that can be referenced within the expression, making complex calculations more readable and preventing redundant computations.
1816

1917
## Syntax
2018

21-
The syntax for the `$let` operator is as follows:
22-
2319
```javascript
2420
{
2521
$let: {
@@ -35,81 +31,122 @@ The syntax for the `$let` operator is as follows:
3531

3632
## Parameters
3733

38-
| | Description |
34+
| Parameter | Description |
3935
| --- | --- |
4036
| **`vars`** | An object that defines the variables and their values. Each variable is assigned the result of an expression. |
4137
| **`in`** | The expression that uses the variables defined in the `vars` object. Variables are referenced using `$$<variable_name>`. |
4238

43-
## Example
39+
## Examples
4440

45-
Let's understand the usage with sample json from `stores` dataset.
41+
Consider this sample document from the stores collection.
4642

4743
```json
4844
{
49-
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
50-
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
51-
"location": {
52-
"lat": 70.1272,
53-
"lon": 69.7296
54-
},
55-
"staff": {
56-
"totalStaff": {
57-
"fullTime": 5,
58-
"partTime": 20
59-
}
60-
},
61-
"sales": {
62-
"totalSales": 151864,
63-
"salesByCategory": [
64-
{
65-
"categoryName": "Sound Bars",
66-
"totalSales": 2120
67-
},
68-
{
69-
"categoryName": "Home Theater Projectors",
70-
"totalSales": 45004
71-
},
72-
{
73-
"categoryName": "Game Controllers",
74-
"totalSales": 43522
75-
},
76-
{
77-
"categoryName": "Remote Controls",
78-
"totalSales": 28946
79-
},
80-
{
81-
"categoryName": "VR Games",
82-
"totalSales": 32272
83-
}
84-
]
85-
},
86-
"promotionEvents": [
87-
{
88-
"eventName": "Discount Delight Days",
89-
"promotionalDates": {
90-
"startDate": {
91-
"Year": 2023,
92-
"Month": 12,
93-
"Day": 26
94-
},
95-
"endDate": {
96-
"Year": 2024,
97-
"Month": 1,
98-
"Day": 5
45+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
46+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
47+
"location": {
48+
"lat": -89.2384,
49+
"lon": -46.4012
50+
},
51+
"staff": {
52+
"totalStaff": {
53+
"fullTime": 8,
54+
"partTime": 20
9955
}
100-
},
101-
"discounts": [
56+
},
57+
"sales": {
58+
"totalSales": 75670,
59+
"salesByCategory": [
60+
{
61+
"categoryName": "Wine Accessories",
62+
"totalSales": 34440
63+
},
64+
{
65+
"categoryName": "Bitters",
66+
"totalSales": 39496
67+
},
68+
{
69+
"categoryName": "Rum",
70+
"totalSales": 1734
71+
}
72+
]
73+
},
74+
"promotionEvents": [
10275
{
103-
"categoryName": "Game Controllers",
104-
"discountPercentage": 22
76+
"eventName": "Unbeatable Bargain Bash",
77+
"promotionalDates": {
78+
"startDate": {
79+
"Year": 2024,
80+
"Month": 6,
81+
"Day": 23
82+
},
83+
"endDate": {
84+
"Year": 2024,
85+
"Month": 7,
86+
"Day": 2
87+
}
88+
},
89+
"discounts": [
90+
{
91+
"categoryName": "Whiskey",
92+
"discountPercentage": 7
93+
},
94+
{
95+
"categoryName": "Bitters",
96+
"discountPercentage": 15
97+
},
98+
{
99+
"categoryName": "Brandy",
100+
"discountPercentage": 8
101+
},
102+
{
103+
"categoryName": "Sports Drinks",
104+
"discountPercentage": 22
105+
},
106+
{
107+
"categoryName": "Vodka",
108+
"discountPercentage": 19
109+
}
110+
]
105111
},
106112
{
107-
"categoryName": "Home Theater Projectors",
108-
"discountPercentage": 23
113+
"eventName": "Steal of a Deal Days",
114+
"promotionalDates": {
115+
"startDate": {
116+
"Year": 2024,
117+
"Month": 9,
118+
"Day": 21
119+
},
120+
"endDate": {
121+
"Year": 2024,
122+
"Month": 9,
123+
"Day": 29
124+
}
125+
},
126+
"discounts": [
127+
{
128+
"categoryName": "Organic Wine",
129+
"discountPercentage": 19
130+
},
131+
{
132+
"categoryName": "White Wine",
133+
"discountPercentage": 20
134+
},
135+
{
136+
"categoryName": "Sparkling Wine",
137+
"discountPercentage": 19
138+
},
139+
{
140+
"categoryName": "Whiskey",
141+
"discountPercentage": 17
142+
},
143+
{
144+
"categoryName": "Vodka",
145+
"discountPercentage": 23
146+
}
147+
]
109148
}
110-
]
111-
}
112-
]
149+
]
113150
}
114151
```
115152

@@ -142,7 +179,7 @@ db.stores.aggregate([
142179
])
143180
```
144181

145-
This will produce the following output:
182+
This query returns the following result.
146183

147184
```json
148185
[
@@ -199,7 +236,7 @@ db.stores.aggregate([
199236
])
200237
```
201238

202-
This will produce comprehensive location analysis with nested calculations.
239+
This query returns the following result.
203240

204241
```json
205242
[

0 commit comments

Comments
 (0)