Skip to content

Commit d492823

Browse files
authored
Revise examples and descriptions for $anyElementTrue
1 parent 73bdbeb commit d492823

File tree

1 file changed

+139
-72
lines changed

1 file changed

+139
-72
lines changed

articles/cosmos-db/mongodb/vcore/operators/set-expression/$anyelementtrue.md

Lines changed: 139 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -28,55 +28,123 @@ The `$anyElementTrue` operator evaluates an array as a set and returns `true` if
2828
| --- | --- |
2929
| `array` | An array of expressions to evaluate. If the array is empty, `$anyElementTrue` returns `false`. |
3030

31-
## Example
31+
## Examples
3232

33-
Let's understand the usage with sample JSON from the `stores` dataset.
33+
Consider this sample document from the stores collection.
3434

3535
```json
3636
{
37-
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
38-
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
39-
"location": {
40-
"lat": 70.1272,
41-
"lon": 69.7296
42-
},
43-
"staff": {
44-
"totalStaff": {
45-
"fullTime": 19,
46-
"partTime": 20
47-
}
48-
},
49-
"sales": {
50-
"totalSales": 151864,
51-
"salesByCategory": [
52-
{
53-
"categoryName": "Sound Bars",
54-
"totalSales": 2120
55-
},
56-
{
57-
"categoryName": "Home Theater Projectors",
58-
"totalSales": 45004
59-
},
60-
{
61-
"categoryName": "Game Controllers",
62-
"totalSales": 43522
63-
},
64-
{
65-
"categoryName": "Remote Controls",
66-
"totalSales": 28946
67-
},
68-
{
69-
"categoryName": "VR Games",
70-
"totalSales": 32272
71-
}
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
47+
}
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": [
67+
{
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+
]
103+
},
104+
{
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+
]
140+
}
72141
]
73-
}
74142
}
75143
```
76144

77145
### Example 1: Determine if any sales category exceeds a target
78146

79-
This following example helps you determine if any sales category exceeds a target. In this case, the target is 40,000 in sales.
147+
This query determines if any sales category exceeds a specified target. In this case, the target is 40,000 in sales.
80148

81149
```javascript
82150
db.stores.aggregate([
@@ -100,38 +168,40 @@ db.stores.aggregate([
100168
])
101169
```
102170

103-
The query returns `true` for the `hasHighPerformingCategory` field, because sales for one of the categories is more than 40,000.
171+
This query returns the following result.
104172

105173
```json
106-
{
107-
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
108-
"salesByCategory": [
109-
{
110-
"categoryName": "Sound Bars",
111-
"totalSales": 2120,
112-
"lastUpdated": "2025-06-11T11:10:34.414Z"
113-
},
114-
null,
115-
{
116-
"categoryName": "Game Controllers",
117-
"totalSales": 43522
118-
},
119-
{
120-
"categoryName": "Remote Controls",
121-
"totalSales": 28946
122-
},
123-
{
124-
"categoryName": "VR Games",
125-
"totalSales": 32272
126-
}
127-
],
128-
"hasHighPerformingCategory": true
129-
}
174+
[
175+
{
176+
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
177+
"salesByCategory": [
178+
{
179+
"categoryName": "Sound Bars",
180+
"totalSales": 2120,
181+
"lastUpdated": "2025-06-11T11:10:34.414Z"
182+
},
183+
null,
184+
{
185+
"categoryName": "Game Controllers",
186+
"totalSales": 43522
187+
},
188+
{
189+
"categoryName": "Remote Controls",
190+
"totalSales": 28946
191+
},
192+
{
193+
"categoryName": "VR Games",
194+
"totalSales": 32272
195+
}
196+
],
197+
"hasHighPerformingCategory": true
198+
}
199+
]
130200
```
131201

132202
### Example 2: Determine if any promotion event has high discounts
133203

134-
The following example helps you determine if any promotion event offers discounts over 20%.
204+
This query determines if any promotion event offers discounts over 20%.
135205

136206
```javascript
137207
db.stores.aggregate([
@@ -156,14 +226,15 @@ db.stores.aggregate([
156226
])
157227
```
158228

159-
The query returns `true`, because there's at least one promotion event with discounts over 20%.
229+
The first five results returned by this query are:
160230

161231
```json
232+
[
162233
{
163234
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
164235
"eventName": "Massive Markdown Mania",
165236
"hasHighDiscount": true
166-
},
237+
},
167238
{
168239
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
169240
"eventName": "Fantastic Deal Days",
@@ -183,12 +254,8 @@ The query returns `true`, because there's at least one promotion event with disc
183254
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
184255
"eventName": "Grand Deal Days",
185256
"hasHighDiscount": true
186-
},
187-
{
188-
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
189-
"eventName": "Major Bargain Bash",
190-
"hasHighDiscount": true
191257
}
258+
]
192259
```
193260

194261
## Related content

0 commit comments

Comments
 (0)