$setUnion (set 表达式)

适用对象: MongoDB vCore

运算符 $setUnion 返回一个数组,其中包含输入数组中的所有唯一元素。 它将数组视为集,删除重复项并忽略元素顺序。 结果仅包含每个唯一元素一次,而不考虑它出现在输入数组中的次数。

语法

$setUnion 运算符的语法如下:

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

参数

DESCRIPTION
<array1>, <array2>, ... 要组合的两个或多个数组。 每个数组被视为一个集,并且从最终结果中删除重复项。

示例:

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

{
  "_id": "26afb024-53c7-4e94-988c-5eede72277d5",
  "name": "First Up Consultants | Microphone Bazaar - South Lexusland",
  "location": {
    "lat": -29.1866,
    "lon": -112.7858
  },
  "staff": {
    "totalStaff": {
      "fullTime": 14,
      "partTime": 8
    }
  },
  "sales": {
    "totalSales": 83865,
    "salesByCategory": [
      {
        "categoryName": "Lavalier Microphones",
        "totalSales": 44174
      },
      {
        "categoryName": "Wireless Microphones",
        "totalSales": 39691
      }
    ]
  },
  "promotionEvents": [
    {
      "eventName": "Price Cut Spectacular",
      "promotionalDates": {
        "startDate": {
          "Year": 2023,
          "Month": 12,
          "Day": 26
        },
        "endDate": {
          "Year": 2024,
          "Month": 1,
          "Day": 5
        }
      },
      "discounts": [
        {
          "categoryName": "Condenser Microphones",
          "discountPercentage": 5
        },
        {
          "categoryName": "Dynamic Microphones",
          "discountPercentage": 14
        }
      ]
    },
    {
      "eventName": "Bargain Bonanza",
      "promotionalDates": {
        "startDate": {
          "Year": 2024,
          "Month": 3,
          "Day": 25
        },
        "endDate": {
          "Year": 2024,
          "Month": 4,
          "Day": 3
        }
      },
      "discounts": [
        {
          "categoryName": "Streaming Microphones",
          "discountPercentage": 14
        },
        {
          "categoryName": "Microphone Stands",
          "discountPercentage": 14
        }
      ]
    },
    {
      "eventName": "Super Savings Extravaganza",
      "promotionalDates": {
        "startDate": {
          "Year": 2024,
          "Month": 6,
          "Day": 23
        },
        "endDate": {
          "Year": 2024,
          "Month": 6,
          "Day": 30
        }
      },
      "discounts": [
        {
          "categoryName": "Condenser Microphones",
          "discountPercentage": 7
        },
        {
          "categoryName": "Microphone Stands",
          "discountPercentage": 5
        }
      ]
    }
  ]
}

示例 1:合并所有产品类别

该示例获取商店处理的所有唯一产品类别的完整列表,包括销售和促销类别。

db.stores.aggregate([
  { $match: {"_id": "26afb024-53c7-4e94-988c-5eede72277d5"} },
  {
    $project: {
      name: 1,
      salesCategories: "$sales.salesByCategory.categoryName",
      firstPromotionCategories: { $arrayElemAt: ["$promotionEvents.discounts.categoryName", 0] },
      secondPromotionCategories: { $arrayElemAt: ["$promotionEvents.discounts.categoryName", 1] },
      thirdPromotionCategories: { $arrayElemAt: ["$promotionEvents.discounts.categoryName", 2] },
      allUniqueCategories: {
        $setUnion: [
          "$sales.salesByCategory.categoryName",
          { $arrayElemAt: ["$promotionEvents.discounts.categoryName", 0] },
          { $arrayElemAt: ["$promotionEvents.discounts.categoryName", 1] },
          { $arrayElemAt: ["$promotionEvents.discounts.categoryName", 2] }
        ]
      }
    }
  }
])

该查询返回销售和促销的所有唯一类别。

{
  "_id": "26afb024-53c7-4e94-988c-5eede72277d5",
  "name": "First Up Consultants | Microphone Bazaar - South Lexusland",
  "salesCategories": [
    "Lavalier Microphones",
    "Wireless Microphones"
  ],
  "firstPromotionCategories": [
    "Condenser Microphones",
    "Dynamic Microphones"
  ],
  "secondPromotionCategories": [
    "Streaming Microphones",
    "Microphone Stands"
  ],
  "thirdPromotionCategories": [
    "Condenser Microphones",
    "Microphone Stands"
  ],
  "allUniqueCategories": [
    "Lavalier Microphones",
    "Wireless Microphones",
    "Condenser Microphones",
    "Dynamic Microphones",
    "Streaming Microphones",
    "Microphone Stands"
  ]
}