$setIntersection

연산자는 $setIntersection 모든 입력 배열에 나타나는 요소가 포함된 배열을 반환합니다. 배열을 집합으로 처리합니다. 즉, 중복 항목을 제거하고 요소의 순서를 무시합니다.

문법

{
  $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": []
   }
]