$bitXor

연산자는 $bitXor 정수 값에 대해 XOR(비트 배타적 OR) 작업을 수행합니다. XOR 연산은 피연산자의 해당 비트가 다른 각 비트 위치에 대해 1을 반환하고 0을 반환합니다.

문법

{
  $bitXor: [ <expression1>, <expression2>, ... ]
}

매개 변수

매개 변수 Description
expression1, expression2, ... 정수 값으로 확인되는 식입니다. 연산자는 이러한 값에 대해 XOR 연산을 순서대로 수행합니다.

예시

스토어 컬렉션에서 이 샘플 문서를 고려합니다.

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

예제 1: 기본 XOR 작업

이 쿼리는 집계 파이프라인을 사용하여 특정 저장소에 대한 정규직 및 파트타임 직원 수를 계산합니다. 결과 문서에는 계산 필드와 함께 저장소 세부 정보가 포함됩니다. 19(이진: 10011)에서 20(이진: 10100) 사이의 XOR 연산은 7(이진: 00111)으로 발생합니다.

db.stores.aggregate([{
        $match: {
            _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"
        }
    },
    {
        $project: {
            name: 1,
            fullTimeStaff: "$staff.totalStaff.fullTime",
            partTimeStaff: "$staff.totalStaff.partTime",
            staffXor: {
                $bitXor: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"]
            }
        }
    }
])

이 쿼리는 다음 결과를 반환합니다.

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

예제 2: 여러 값이 있는 XOR

이 쿼리는 특정 저장소의 이벤트에 대한 Discount Delight Days 모든 할인 비율의 비트 XOR를 계산합니다. 결과 문서는 이벤트에 대한 모든 할인 비율의 비트 XOR 계산을 Discount Delight Days 나타냅니다.

db.stores.aggregate([{
        $match: {
            _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"
        }
    },
    {
        $unwind: "$promotionEvents"
    },
    {
        $match: {
            "promotionEvents.eventName": "Discount Delight Days"
        }
    },
    {
        $unwind: "$promotionEvents.discounts"
    },
    {
        $group: {
            _id: "$_id",
            name: {
                $first: "$name"
            },
            eventName: {
                $first: "$promotionEvents.eventName"
            },
            discountPercentages: {
                $push: "$promotionEvents.discounts.discountPercentage"
            }
        }
    },
    {
        $project: {
            name: 1,
            eventName: 1,
            discountPercentages: 1,
            xorResult: {
                $reduce: {
                    input: {
                        $map: {
                            input: "$discountPercentages",
                            as: "val",
                            in: {
                                $toLong: "$$val"
                            }
                        }
                    },
                    initialValue: {
                        $toLong: 0
                    },
                    in: {
                        $bitXor: ["$$value", "$$this"]
                    }
                }
            }
        }
    }
])

이 쿼리는 다음 결과를 반환합니다.

[
  {
    "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
    "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
    "eventName": "Discount Delight Days",
    "discountPercentages": [22, 23, 10, 10, 9, 24],
    "xorResult": { "$numberLong": "16" }
  }
]