Skip to content

Commit 59dd080

Browse files
authored
Revise $setDifference documentation with new examples
1 parent d492823 commit 59dd080

File tree

1 file changed

+157
-94
lines changed

1 file changed

+157
-94
lines changed

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

Lines changed: 157 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
ms.service: azure-cosmos-db
88
ms.subservice: mongodb-vcore
99
ms.topic: language-reference
10-
ms.date: 06/09/2025
10+
ms.date: 09/04/2025
1111
---
1212

1313
# $setDifference
@@ -29,63 +29,122 @@ The `$setDifference` operator returns a set that includes elements that exist in
2929
| `array1` | The first array to compare. Elements unique to this array are returned. |
3030
| `array2` | The second array to compare against the first array. Elements that exist in both arrays are excluded from the result. |
3131

32-
## Example
32+
## Examples
3333

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

3636
```json
3737
{
38-
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
39-
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
40-
"sales": {
41-
"salesByCategory": [
42-
{
43-
"categoryName": "Sound Bars",
44-
"totalSales": 2120
45-
},
46-
{
47-
"categoryName": "Home Theater Projectors",
48-
"totalSales": 45004
49-
},
50-
{
51-
"categoryName": "Game Controllers",
52-
"totalSales": 43522
53-
},
54-
{
55-
"categoryName": "Remote Controls",
56-
"totalSales": 28946
57-
},
58-
{
59-
"categoryName": "VR Games",
60-
"totalSales": 32272
61-
}
62-
]
63-
},
64-
"promotionEvents": [
65-
{
66-
"eventName": "Massive Markdown Mania",
67-
"discounts": [
68-
{
69-
"categoryName": "DVD Players",
70-
"discountPercentage": 14
71-
},
38+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
39+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
40+
"location": {
41+
"lat": -89.2384,
42+
"lon": -46.4012
43+
},
44+
"staff": {
45+
"totalStaff": {
46+
"fullTime": 8,
47+
"partTime": 20
48+
}
49+
},
50+
"sales": {
51+
"totalSales": 75670,
52+
"salesByCategory": [
53+
{
54+
"categoryName": "Wine Accessories",
55+
"totalSales": 34440
56+
},
57+
{
58+
"categoryName": "Bitters",
59+
"totalSales": 39496
60+
},
61+
{
62+
"categoryName": "Rum",
63+
"totalSales": 1734
64+
}
65+
]
66+
},
67+
"promotionEvents": [
7268
{
73-
"categoryName": "Media Players",
74-
"discountPercentage": 21
69+
"eventName": "Unbeatable Bargain Bash",
70+
"promotionalDates": {
71+
"startDate": {
72+
"Year": 2024,
73+
"Month": 6,
74+
"Day": 23
75+
},
76+
"endDate": {
77+
"Year": 2024,
78+
"Month": 7,
79+
"Day": 2
80+
}
81+
},
82+
"discounts": [
83+
{
84+
"categoryName": "Whiskey",
85+
"discountPercentage": 7
86+
},
87+
{
88+
"categoryName": "Bitters",
89+
"discountPercentage": 15
90+
},
91+
{
92+
"categoryName": "Brandy",
93+
"discountPercentage": 8
94+
},
95+
{
96+
"categoryName": "Sports Drinks",
97+
"discountPercentage": 22
98+
},
99+
{
100+
"categoryName": "Vodka",
101+
"discountPercentage": 19
102+
}
103+
]
75104
},
76105
{
77-
"categoryName": "Televisions",
78-
"discountPercentage": 22
106+
"eventName": "Steal of a Deal Days",
107+
"promotionalDates": {
108+
"startDate": {
109+
"Year": 2024,
110+
"Month": 9,
111+
"Day": 21
112+
},
113+
"endDate": {
114+
"Year": 2024,
115+
"Month": 9,
116+
"Day": 29
117+
}
118+
},
119+
"discounts": [
120+
{
121+
"categoryName": "Organic Wine",
122+
"discountPercentage": 19
123+
},
124+
{
125+
"categoryName": "White Wine",
126+
"discountPercentage": 20
127+
},
128+
{
129+
"categoryName": "Sparkling Wine",
130+
"discountPercentage": 19
131+
},
132+
{
133+
"categoryName": "Whiskey",
134+
"discountPercentage": 17
135+
},
136+
{
137+
"categoryName": "Vodka",
138+
"discountPercentage": 23
139+
}
140+
]
79141
}
80-
]
81-
}
82-
]
142+
]
83143
}
84144
```
85-
86145
### Example 1: Find categories of products for sale but not discounted
87146

88-
The following example checks for product categories that include sales data but no discounts.
147+
This query retrieves product categories that include sales data but no discounts.
89148

90149
```javascript
91150
db.stores.aggregate([
@@ -118,39 +177,41 @@ db.stores.aggregate([
118177
])
119178
```
120179

121-
The query output shows categories of products that are sold but never discounted.
180+
This query returns the following result.
122181

123182
```json
124-
{
125-
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
126-
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
127-
"soldCategories": [
128-
"Sound Bars",
129-
"Game Controllers",
130-
"Remote Controls",
131-
"VR Games"
132-
],
133-
"discountedCategories": [
134-
"DVD Players",
135-
"Projector Lamps",
136-
"Media Players",
137-
"Blu-ray Players",
138-
"Home Theater Systems",
139-
"Televisions"
140-
],
141-
"categoriesWithoutDiscounts": [
142-
"Sound Bars",
143-
"Home Theater Projectors",
144-
"Game Controllers",
145-
"Remote Controls",
146-
"VR Games"
147-
]
148-
}
183+
[
184+
{
185+
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
186+
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
187+
"soldCategories": [
188+
"Sound Bars",
189+
"Game Controllers",
190+
"Remote Controls",
191+
"VR Games"
192+
],
193+
"discountedCategories": [
194+
"DVD Players",
195+
"Projector Lamps",
196+
"Media Players",
197+
"Blu-ray Players",
198+
"Home Theater Systems",
199+
"Televisions"
200+
],
201+
"categoriesWithoutDiscounts": [
202+
"Sound Bars",
203+
"Home Theater Projectors",
204+
"Game Controllers",
205+
"Remote Controls",
206+
"VR Games"
207+
]
208+
}
209+
]
149210
```
150211

151212
### Example 2: Compare staff distribution types
152213

153-
The following example demonstrates how to find the difference between two hypothetical staff requirement lists.
214+
This query finds the difference between two hypothetical staff requirement lists.
154215

155216
```javascript
156217
db.stores.aggregate([
@@ -171,29 +232,31 @@ db.stores.aggregate([
171232
])
172233
```
173234

174-
The query returns the skills that are required but not available.
235+
This query returns the following result.
175236

176237
```json
177-
{
178-
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
179-
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
180-
"requiredSkills": [
181-
"Sales",
182-
"Customer Service",
183-
"Technical Support",
184-
"Inventory Management"
185-
],
186-
"availableSkills": [
187-
"Sales",
188-
"Customer Service",
189-
"Marketing",
190-
"Administration"
191-
],
192-
"missingSkills": [
193-
"Technical Support",
194-
"Inventory Management"
195-
]
196-
}
238+
[
239+
{
240+
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
241+
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
242+
"requiredSkills": [
243+
"Sales",
244+
"Customer Service",
245+
"Technical Support",
246+
"Inventory Management"
247+
],
248+
"availableSkills": [
249+
"Sales",
250+
"Customer Service",
251+
"Marketing",
252+
"Administration"
253+
],
254+
"missingSkills": [
255+
"Technical Support",
256+
"Inventory Management"
257+
]
258+
}
259+
]
197260
```
198261

199262
## Related content

0 commit comments

Comments
 (0)