你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

$setIntersection(set 表达式)

适用对象: MongoDB vCore

运算符 $setIntersection 返回一个数组,其中包含显示在所有输入数组中的元素。 它将数组视为集,这意味着它删除重复项并忽略元素的顺序。

语法

运算符的 $isArray 语法如下所示:

{
  $setIntersection: [ <array1>, <array2>, ... ]
}

参数

DESCRIPTION
<array1>, <array2>, ... 要查找其交集的两个或多个数组。 每个数组都被视为一个集。

示例:

让我们了解 stores 数据集中的示例 json 的用法。

{
  "_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",
    "Home Theater Projectors",
    "Game Controllers",
    "Remote Controls",
    "VR Games"
  ],
  "firstPromotionCategories": [
    "DVD Players",
    "Projector Lamps",
    "Media Players",
    "Blu-ray Players",
    "Home Theater Systems",
    "Televisions"
  ],
  "secondPromotionCategories": [
    "Game Controllers",
    "Home Theater Projectors",
    "Sound Bars",
    "Media Players",
    "Televisions",
    "Projector Lamps"
  ],
  "commonSalesAndFirstPromotion": [],
  "commonSalesAndSecondPromotion": [
    "Sound Bars",
    "Home Theater Projectors",
    "Game Controllers"
  ]
}

示例 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] }
        ]
      }
    }
  }
])

查询将返回一个空数组, commonAcrossPromotions 因为所有促销中都不存在常见的产品类别。

 {
    "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
    "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
    "commonAcrossPromotions": []
  }