You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
4
4
description: The $let operator allows defining variables for use in a specified expression, enabling complex calculations and reducing code repetition.
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.
18
16
19
17
## Syntax
20
18
21
-
The syntax for the `$let` operator is as follows:
22
-
23
19
```javascript
24
20
{
25
21
$let: {
@@ -35,81 +31,122 @@ The syntax for the `$let` operator is as follows:
35
31
36
32
## Parameters
37
33
38
-
|| Description |
34
+
|Parameter | Description |
39
35
| --- | --- |
40
36
|**`vars`**| An object that defines the variables and their values. Each variable is assigned the result of an expression. |
41
37
|**`in`**| The expression that uses the variables defined in the `vars` object. Variables are referenced using `$$<variable_name>`. |
42
38
43
-
## Example
39
+
## Examples
44
40
45
-
Let's understand the usage with sample json from `stores` dataset.
41
+
Consider this sample document from the stores collection.
46
42
47
43
```json
48
44
{
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
99
55
}
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": [
102
75
{
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
+
]
105
111
},
106
112
{
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
+
]
109
148
}
110
-
]
111
-
}
112
-
]
149
+
]
113
150
}
114
151
```
115
152
@@ -142,7 +179,7 @@ db.stores.aggregate([
142
179
])
143
180
```
144
181
145
-
This will produce the following output:
182
+
This query returns the following result.
146
183
147
184
```json
148
185
[
@@ -199,7 +236,7 @@ db.stores.aggregate([
199
236
])
200
237
```
201
238
202
-
This will produce comprehensive location analysis with nested calculations.
0 commit comments