运算符 $setIntersection 返回一个数组,其中包含显示在所有输入数组中的元素。 它将数组视为集,这意味着它将删除重复项并忽略元素的顺序。
Syntax
{
$setIntersection: [ <array1>, <array2>, ... ]
}
参数
| 参数 | Description |
|---|---|
<array1>, <array2>, ... |
要查找其交集的两个或多个数组。 每个数组都被视为一个集。 |
例子
请考虑商店集合中的这个示例文档。
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"location": {
"lat": 70.1272,
"lon": 69.7296
},
"staff": {
"totalStaff": {
"fullTime": 19,
"partTime": 20
}
},
"sales": {
"totalSales": 151864,
"salesByCategory": [
{
"categoryName": "Sound Bars",
"totalSales": 2120
},
{
"categoryName": "Home Theater Projectors",
"totalSales": 45004
},
{
"categoryName": "Game Controllers",
"totalSales": 43522
},
{
"categoryName": "Remote Controls",
"totalSales": 28946
},
{
"categoryName": "VR Games",
"totalSales": 32272
}
]
},
"promotionEvents": [
{
"eventName": "Massive Markdown Mania",
"promotionalDates": {
"startDate": {
"Year": 2023,
"Month": 6,
"Day": 29
},
"endDate": {
"Year": 2023,
"Month": 7,
"Day": 9
}
},
"discounts": [
{
"categoryName": "DVD Players",
"discountPercentage": 14
},
{
"categoryName": "Projector Lamps",
"discountPercentage": 6
},
{
"categoryName": "Media Players",
"discountPercentage": 21
},
{
"categoryName": "Blu-ray Players",
"discountPercentage": 21
},
{
"categoryName": "Home Theater Systems",
"discountPercentage": 5
},
{
"categoryName": "Televisions",
"discountPercentage": 22
}
]
},
{
"eventName": "Discount Delight Days",
"promotionalDates": {
"startDate": {
"Year": 2023,
"Month": 12,
"Day": 26
},
"endDate": {
"Year": 2024,
"Month": 1,
"Day": 5
}
},
"discounts": [
{
"categoryName": "Game Controllers",
"discountPercentage": 22
},
{
"categoryName": "Home Theater Projectors",
"discountPercentage": 23
},
{
"categoryName": "Sound Bars",
"discountPercentage": 10
},
{
"categoryName": "Media Players",
"discountPercentage": 10
},
{
"categoryName": "Televisions",
"discountPercentage": 9
},
{
"categoryName": "Projector Lamps",
"discountPercentage": 24
}
]
}
]
}
示例 1:查找销售和促销之间的常见类别
此查询确定在商店的销售数据和促销折扣中显示的产品类别。
db.stores.aggregate([
{ $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
{
$project: {
name: 1,
salesCategories: "$sales.salesByCategory.categoryName",
firstPromotionCategories: { $arrayElemAt: ["$promotionEvents.discounts.categoryName", 0] },
secondPromotionCategories: { $arrayElemAt: ["$promotionEvents.discounts.categoryName", 1] },
commonSalesAndFirstPromotion: {
$setIntersection: [
"$sales.salesByCategory.categoryName",
{ $arrayElemAt: ["$promotionEvents.discounts.categoryName", 0] }
]
},
commonSalesAndSecondPromotion: {
$setIntersection: [
"$sales.salesByCategory.categoryName",
{ $arrayElemAt: ["$promotionEvents.discounts.categoryName", 1] }
]
}
}
}
])
此查询返回以下结果。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"salesCategories": [
"Sound Bars",
"Game Controllers",
"Remote Controls",
"VR Games"
],
"firstPromotionCategories": [
"DVD Players",
"Projector Lamps",
"Media Players",
"Blu-ray Players",
"Home Theater Systems",
"Televisions"
],
"secondPromotionCategories": [
"TV Mounts",
"Game Accessories",
"Portable Projectors",
"Projector Screens",
"Blu-ray Players",
"DVD Players"
],
"commonSalesAndFirstPromotion": [],
"commonSalesAndSecondPromotion": []
}
]
示例 2:跨多个促销事件查找常见类别
此查询提取显示在多个促销事件中的类别。
db.stores.aggregate([
{ $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
{
$project: {
name: 1,
commonAcrossPromotions: {
$setIntersection: [
{ $arrayElemAt: ["$promotionEvents.discounts.categoryName", 0] },
{ $arrayElemAt: ["$promotionEvents.discounts.categoryName", 1] },
{ $arrayElemAt: ["$promotionEvents.discounts.categoryName", 2] }
]
}
}
}
])
此查询返回以下结果。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"commonAcrossPromotions": []
}
]
相关内容
- 查看用于 从 MongoDB 迁移到 Azure DocumentDB 的选项。
- 详细了解 与 MongoDB 的功能兼容性。