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
@@ -28,66 +28,123 @@ The `$literal` operator returns the specified value without parsing it as an exp
28
28
| --- | --- |
29
29
|**`<value>`**| Any value that should be returned as-is without interpretation. This can be a string, number, boolean, array, object, or null. |
30
30
31
-
## Example
31
+
## Examples
32
32
33
-
Let's understand the usage with sample json from `stores` dataset.
33
+
Consider this sample document from the stores collection.
34
34
35
35
```json
36
36
{
37
-
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
38
-
"name": "First Up Consultants | Bed and Bath Center - South Amir",
39
-
"location": {
40
-
"lat": 60.7954,
41
-
"lon": -142.0012
42
-
},
43
-
"staff": {
44
-
"totalStaff": {
45
-
"fullTime": 18,
46
-
"partTime": 17
47
-
}
48
-
},
49
-
"sales": {
50
-
"totalSales": 37701,
51
-
"salesByCategory": [
52
-
{
53
-
"categoryName": "Mattress Toppers",
54
-
"totalSales": 37701
55
-
}
56
-
]
57
-
},
58
-
"promotionEvents": [
59
-
{
60
-
"eventName": "Price Drop Palooza",
61
-
"promotionalDates": {
62
-
"startDate": {
63
-
"Year": 2024,
64
-
"Month": 9,
65
-
"Day": 21
66
-
},
67
-
"endDate": {
68
-
"Year": 2024,
69
-
"Month": 9,
70
-
"Day": 30
37
+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
38
+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
39
+
"location": {
40
+
"lat": -89.2384,
41
+
"lon": -46.4012
42
+
},
43
+
"staff": {
44
+
"totalStaff": {
45
+
"fullTime": 8,
46
+
"partTime": 20
71
47
}
72
-
},
73
-
"discounts": [
48
+
},
49
+
"sales": {
50
+
"totalSales": 75670,
51
+
"salesByCategory": [
52
+
{
53
+
"categoryName": "Wine Accessories",
54
+
"totalSales": 34440
55
+
},
56
+
{
57
+
"categoryName": "Bitters",
58
+
"totalSales": 39496
59
+
},
60
+
{
61
+
"categoryName": "Rum",
62
+
"totalSales": 1734
63
+
}
64
+
]
65
+
},
66
+
"promotionEvents": [
74
67
{
75
-
"categoryName": "Bath Accessories",
76
-
"discountPercentage": 18
68
+
"eventName": "Unbeatable Bargain Bash",
69
+
"promotionalDates": {
70
+
"startDate": {
71
+
"Year": 2024,
72
+
"Month": 6,
73
+
"Day": 23
74
+
},
75
+
"endDate": {
76
+
"Year": 2024,
77
+
"Month": 7,
78
+
"Day": 2
79
+
}
80
+
},
81
+
"discounts": [
82
+
{
83
+
"categoryName": "Whiskey",
84
+
"discountPercentage": 7
85
+
},
86
+
{
87
+
"categoryName": "Bitters",
88
+
"discountPercentage": 15
89
+
},
90
+
{
91
+
"categoryName": "Brandy",
92
+
"discountPercentage": 8
93
+
},
94
+
{
95
+
"categoryName": "Sports Drinks",
96
+
"discountPercentage": 22
97
+
},
98
+
{
99
+
"categoryName": "Vodka",
100
+
"discountPercentage": 19
101
+
}
102
+
]
77
103
},
78
104
{
79
-
"categoryName": "Pillow Top Mattresses",
80
-
"discountPercentage": 17
105
+
"eventName": "Steal of a Deal Days",
106
+
"promotionalDates": {
107
+
"startDate": {
108
+
"Year": 2024,
109
+
"Month": 9,
110
+
"Day": 21
111
+
},
112
+
"endDate": {
113
+
"Year": 2024,
114
+
"Month": 9,
115
+
"Day": 29
116
+
}
117
+
},
118
+
"discounts": [
119
+
{
120
+
"categoryName": "Organic Wine",
121
+
"discountPercentage": 19
122
+
},
123
+
{
124
+
"categoryName": "White Wine",
125
+
"discountPercentage": 20
126
+
},
127
+
{
128
+
"categoryName": "Sparkling Wine",
129
+
"discountPercentage": 19
130
+
},
131
+
{
132
+
"categoryName": "Whiskey",
133
+
"discountPercentage": 17
134
+
},
135
+
{
136
+
"categoryName": "Vodka",
137
+
"discountPercentage": 23
138
+
}
139
+
]
81
140
}
82
-
]
83
-
}
84
-
]
141
+
]
85
142
}
86
143
```
87
144
88
145
### Example 1: Adding literal status fields
89
146
90
-
The example demonstrates adding literal status information to store documents, including literal strings that might otherwise be interpreted as field names or operators.
147
+
This query demonstrates adding literal status information to store documents, including literal strings that might otherwise be interpreted as field names or operators.
91
148
92
149
```javascript
93
150
db.stores.aggregate([
@@ -110,26 +167,28 @@ db.stores.aggregate([
110
167
])
111
168
```
112
169
113
-
The query adds literal values to each store document. Note how `$pending` is treated as a literal string rather than a field reference.
170
+
This query returns the following result.
114
171
115
172
```json
116
-
{
117
-
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
118
-
"name": "First Up Consultants | Bed and Bath Center - South Amir",
119
-
"totalSales": 37701,
120
-
"status": "Active",
121
-
"reviewStatus": "$pending",
122
-
"priority": 1,
123
-
"metadata": {
124
-
"lastUpdated": "2024-06-13",
125
-
"source": "automated-system"
173
+
[
174
+
{
175
+
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
176
+
"name": "First Up Consultants | Bed and Bath Center - South Amir",
177
+
"totalSales": 37701,
178
+
"status": "Active",
179
+
"reviewStatus": "$pending",
180
+
"priority": 1,
181
+
"metadata": {
182
+
"lastUpdated": "2024-06-13",
183
+
"source": "automated-system"
184
+
}
126
185
}
127
-
}
186
+
]
128
187
```
129
188
130
189
### Example 2: Creating literal arrays and conditional values
131
190
132
-
This example shows how to use `$literal` with arrays and in conditional expressions to ensure values are not interpreted as operators.
191
+
This query shows how to use `$literal` with arrays and in conditional expressions to ensure values are not interpreted as operators.
133
192
134
193
```javascript
135
194
db.stores.aggregate([
@@ -157,25 +216,27 @@ db.stores.aggregate([
157
216
])
158
217
```
159
218
160
-
The query creates literal arrays and objects that contain strings starting with `$`, which would normally be interpreted as operators or field references.
219
+
This query returns the following result.
161
220
162
221
```json
163
-
{
164
-
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
165
-
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
166
-
"totalSales": 160000,
167
-
"performanceCategory": "High Performer",
168
-
"tags": ["$featured", "$promoted", "$new"],
169
-
"searchKeywords": {
170
-
"$or": ["electronics", "entertainment"],
171
-
"$and": ["home", "theater"]
222
+
[
223
+
{
224
+
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
225
+
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
226
+
"totalSales": 160000,
227
+
"performanceCategory": "High Performer",
228
+
"tags": ["$featured", "$promoted", "$new"],
229
+
"searchKeywords": {
230
+
"$or": ["electronics", "entertainment"],
231
+
"$and": ["home", "theater"]
232
+
}
172
233
}
173
-
}
234
+
]
174
235
```
175
236
176
237
### Example 3: Literal null and boolean values
177
238
178
-
This example demonstrates using `$literal` with null values and booleans, especially useful when these values need to be explicitly set rather than computed.
239
+
This query demonstrates using `$literal` with null values and booleans, especially useful when these values need to be explicitly set rather than computed.
179
240
180
241
```javascript
181
242
db.stores.aggregate([
@@ -200,23 +261,25 @@ db.stores.aggregate([
200
261
])
201
262
```
202
263
203
-
The query adds explicit literal values including null, booleans, and strings that look like MongoDB expressions.
264
+
This query returns the following result.
204
265
205
266
```json
206
-
{
207
-
"_id": "e6895a31-a5cd-4103-8889-3b95a864e5a6",
208
-
"name": "VanArsdel, Ltd. | Picture Frame Store - Port Clevelandton",
0 commit comments