如果两个集具有相同的不同元素,而不考虑顺序或重复项,则 $setEquals 运算符返回 true 。 它将数组视为集,并忽略重复的值和元素顺序。
Syntax
{
$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
}
]
相关内容
- 查看用于 从 MongoDB 迁移到 Azure DocumentDB 的选项。
- 详细了解 与 MongoDB 的功能兼容性。