$setEquals 순서나 중복에 관계없이 두 집합에 동일한 고유 요소가 있으면 연산자가 반환 true 됩니다. 배열을 집합으로 처리하고 중복 값 및 요소 순서를 무시합니다.
문법
{
$setEquals: [ <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": "26afb024-53c7-4e94-988c-5eede72277d5"} },
{
$project: {
name: 1,
event1Categories: { $arrayElemAt: ["$promotionEvents.discounts.categoryName", 0] },
event2Categories: { $arrayElemAt: ["$promotionEvents.discounts.categoryName", 1] },
sameDiscountCategories: {
$setEquals: [
{ $arrayElemAt: ["$promotionEvents.discounts.categoryName", 0] },
{ $arrayElemAt: ["$promotionEvents.discounts.categoryName", 1] }
]
}
}
}
])
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
"event1Categories": [
"Condenser Microphones",
"Dynamic Microphones"
],
"event2Categories": [
"Streaming Microphones",
"Microphone Stands"
],
"sameDiscountCategories": false
}
]
예제 2: 직원 요구 사항 비교
이 쿼리는 두 매장의 직원 구조 요구 사항이 동일한지 여부를 결정합니다.
db.stores.aggregate([
{ $match: {"_id": { $in: ["26afb024-53c7-4e94-988c-5eede72277d5", "f2a8c190-28e4-4e14-9d8b-0256e53dca66"] }} },
{
$group: {
_id: null,
stores: { $push: { _id: "$_id", name: "$name" }}
}
},
{
$project: {
store1: { $arrayElemAt: ["$stores", 0] },
store2: { $arrayElemAt: ["$stores", 1] },
staffTypes1: ["fullTime", "partTime"],
staffTypes2: ["fullTime", "partTime"],
sameStaffStructure: {
$setEquals: [
["fullTime", "partTime"],
["fullTime", "partTime"]
]
}
}
}
])
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": null,
"store1": {
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
"name": "First Up Consultants | Microphone Bazaar - South Lexusland"
},
"store2": {
"_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66",
"name": "Fabrikam, Inc. | Car Accessory Outlet - West Adele"
},
"staffTypes1": ["fullTime", "partTime"],
"staffTypes2": ["fullTime", "partTime"],
"sameStaffStructure": true
}
]
예제 3: 집합과 중복 항목 비교
이 쿼리는 연산자를 $setEquals 사용하여 중복 및 순서를 무시합니다.
db.stores.aggregate([
{ $match: {"_id": "26afb024-53c7-4e94-988c-5eede72277d5"} },
{
$project: {
name: 1,
array1: ["Microphones", "Stands", "Microphones", "Accessories"],
array2: ["Stands", "Accessories", "Microphones"],
arraysEqual: {
$setEquals: [
["Microphones", "Stands", "Microphones", "Accessories"],
["Stands", "Accessories", "Microphones"]
]
}
}
}
])
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
"array1": ["Microphones", "Stands", "Microphones", "Accessories"],
"array2": ["Stands", "Accessories", "Microphones"],
"arraysEqual": true
}
]