Notatka
Dostęp do tej strony wymaga autoryzacji. Może spróbować zalogować się lub zmienić katalogi.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
Operator $setDifference zwraca zestaw zawierający elementy, które istnieją w jednym zestawie, ale nie w innym zestawie. Traktuje tablice jako zestawy i ignoruje zduplikowane wartości i kolejność elementów.
Składnia
{
$setDifference: [ <array1>, <array2> ]
}
Parametry
| Parameter | Description |
|---|---|
array1 |
Pierwsza tablica do porównania. Zwracane są elementy unikatowe dla tej tablicy. |
array2 |
Druga tablica do porównania z pierwszą tablicą. Elementy, które istnieją w obu tablicach, są wykluczone z wyniku. |
Przykłady
Rozważmy ten przykładowy dokument z kolekcji sklepów.
{
"_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
}
]
}
]
}
Przykład 1: Znajdowanie kategorii produktów do sprzedaży, ale nie z rabatem
To zapytanie pobiera kategorie produktów, które zawierają dane sprzedaży, ale nie ma rabatów.
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"]
}
}
}
])
To zapytanie zwraca następujący wynik.
[
{
"_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"
]
}
]
Przykład 2. Porównanie typów dystrybucji pracowników
To zapytanie znajduje różnicę między dwiema hipotetyczną listą wymagań personelu.
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"]
]
}
}
}
])
To zapytanie zwraca następujący wynik.
[
{
"_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"
]
}
]