$setDifference運算子會傳回一個集合,其中包含存在於一個集合中但不存在於另一個集合中的元素。 它將陣列視為集合,並忽略重複值和元素順序。
語法
{
$setDifference: [ <array1>, <array2> ]
}
參數
| 參數 | Description |
|---|---|
array1 |
要比較的第一個陣列。 會傳回這個陣列中唯一的專案。 |
array2 |
要與第一個陣列進行比較的第二個陣列。 存在於這兩個陣列中的元素會從結果中排除。 |
範例
請參考商店集合中的此範例檔。
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
範例 1:尋找待售但未打折的產品類別
此查詢會擷取包含銷售資料但不包含折扣的產品類別。
db.stores.aggregate([
{ $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
{
$project: {
name: 1,
soldCategories: "$sales.salesByCategory.categoryName",
discountedCategories: {
$reduce: {
input: "$promotionEvents",
initialValue: [],
in: {
$concatArrays: ["$$value", "$$this.discounts.categoryName"]
}
}
}
}
},
{
$project: {
name: 1,
soldCategories: 1,
discountedCategories: 1,
categoriesWithoutDiscounts: {
$setDifference: ["$soldCategories", "$discountedCategories"]
}
}
}
])
此查詢會傳回下列結果。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"soldCategories": [
"Sound Bars",
"Game Controllers",
"Remote Controls",
"VR Games"
],
"discountedCategories": [
"DVD Players",
"Projector Lamps",
"Media Players",
"Blu-ray Players",
"Home Theater Systems",
"Televisions"
],
"categoriesWithoutDiscounts": [
"Sound Bars",
"Home Theater Projectors",
"Game Controllers",
"Remote Controls",
"VR Games"
]
}
]
範例 2:比較員工分佈類型
此查詢會尋找兩個假設的員工需求清單之間的差異。
db.stores.aggregate([
{ $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
{
$project: {
name: 1,
requiredSkills: ["Sales", "Customer Service", "Technical Support", "Inventory Management"],
availableSkills: ["Sales", "Customer Service", "Marketing", "Administration"],
missingSkills: {
$setDifference: [
["Sales", "Customer Service", "Technical Support", "Inventory Management"],
["Sales", "Customer Service", "Marketing", "Administration"]
]
}
}
}
])
此查詢會傳回下列結果。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"requiredSkills": [
"Sales",
"Customer Service",
"Technical Support",
"Inventory Management"
],
"availableSkills": [
"Sales",
"Customer Service",
"Marketing",
"Administration"
],
"missingSkills": [
"Technical Support",
"Inventory Management"
]
}
]