$設定為子集合

運算 $setIsSubset 子會傳回布林值,指出一個陣列是否是第二個陣列的子集。 它將數組視為集合,這意味著它忽略重複項和元素順序。 如果第一個陣列中的所有元素都存在於第二個陣列中,則會傳回 true 。 否則會傳回 false。

語法

{
  $setIsSubset: [ <array1>, <array2> ]
}

參數

參數 Description
<array1> 要檢查它是否是 的 <array2>子集的陣列。
<array2> 要檢查的陣列。

範例

請參考商店集合中的此範例檔。

{
  "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
  "name": "First Up Consultants | Bed and Bath Center - South Amir",
  "location": {
    "lat": 60.7954,
    "lon": -142.0012
  },
  "staff": {
    "totalStaff": {
      "fullTime": 18,
      "partTime": 17
    }
  },
  "sales": {
    "totalSales": 37701,
    "salesByCategory": [
      {
        "categoryName": "Mattress Toppers",
        "totalSales": 37701
      }
    ]
  },
  "promotionEvents": [
    {
      "eventName": "Price Drop Palooza",
      "promotionalDates": {
        "startDate": {
          "Year": 2024,
          "Month": 9,
          "Day": 21
        },
        "endDate": {
          "Year": 2024,
          "Month": 9,
          "Day": 30
        }
      },
      "discounts": [
        {
          "categoryName": "Bath Accessories",
          "discountPercentage": 18
        },
        {
          "categoryName": "Pillow Top Mattresses",
          "discountPercentage": 17
        },
        {
          "categoryName": "Bathroom Scales",
          "discountPercentage": 9
        },
        {
          "categoryName": "Towels",
          "discountPercentage": 5
        },
        {
          "categoryName": "Bathrobes",
          "discountPercentage": 5
        },
        {
          "categoryName": "Mattress Toppers",
          "discountPercentage": 6
        },
        {
          "categoryName": "Hand Towels",
          "discountPercentage": 9
        },
        {
          "categoryName": "Shower Heads",
          "discountPercentage": 5
        },
        {
          "categoryName": "Bedspreads",
          "discountPercentage": 19
        },
        {
          "categoryName": "Bath Mats",
          "discountPercentage": 21
        }
      ]
    }
  ]
}

範例 1:判斷銷售類別是否為促銷類別的子集

此查詢會判斷商店的所有類別是否包含在其促銷折扣中,反之亦然。 此查詢會傳回銷售和促銷括弧下包含的類別。 它確認該 sales 值是特定促銷類別的子集(但不會執行相反的操作)。

db.stores.aggregate([
  { $match: {"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f"} },
  {
    $project: {
      name: 1,
      salesCategories: "$sales.salesByCategory.categoryName",
      promotionCategories: { $arrayElemAt: ["$promotionEvents.discounts.categoryName", 0] },
      salesAreSubsetOfPromotions: {
        $setIsSubset: [
          "$sales.salesByCategory.categoryName",
          { $arrayElemAt: ["$promotionEvents.discounts.categoryName", 0] }
        ]
      },
      promotionsAreSubsetOfSales: {
        $setIsSubset: [
          { $arrayElemAt: ["$promotionEvents.discounts.categoryName", 0] },
          "$sales.salesByCategory.categoryName"
        ]
      }
    }
  }
])

此查詢會傳回下列結果。

[
  {
    "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
    "name": "First Up Consultants | Bed and Bath Center - South Amir",
    "salesCategories": [
      "Mattress Toppers"
    ],
    "promotionCategories": [
      "Bath Accessories",
      "Pillow Top Mattresses",
      "Bathroom Scales",
      "Towels",
      "Bathrobes",
      "Mattress Toppers",
      "Hand Towels",
      "Shower Heads",
      "Bedspreads",
      "Bath Mats"
    ],
    "salesAreSubsetOfPromotions": true,
    "promotionsAreSubsetOfSales": false
  }
]