$bitOr

$bitOr 연산자는 정수 값에 대해 비트 OR 연산을 수행합니다. 첫 번째 피연산자의 각 비트를 두 번째 피연산자의 해당 비트와 비교합니다. 두 비트 중 하나가 1이면 해당 결과 비트가 1로 설정됩니다. 두 비트가 모두 0이면 해당 결과 비트가 0으로 설정됩니다.

문법

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

매개 변수

매개 변수 Description
expression1, expression2, ... 정수로 계산되는 식입니다. $bitOr 연산자는 제공된 모든 식에 대해 비트 OR 연산을 수행합니다.

예시

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

{
    "_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: 기본 비트 OR 연산

이 쿼리는 특정 저장소 문서의 직원 값에 대해 비트 OR 연산을 수행하여 사용 권한 플래그를 결합합니다. 비트 OR 3(이진의 경우 011) 및 2(이진의 경우 010)는 3(이진의 경우 011)과 같습니다.

db.stores.aggregate([{
        $match: {
            _id: "f2a8c190-28e4-4e14-9d8b-0256e53dca66"
        }
    },
    {
        $project: {
            name: 1,
            fullTimeStaff: "$staff.totalStaff.fullTime",
            partTimeStaff: "$staff.totalStaff.partTime",
            combinedStaffFlag: {
                $bitOr: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"]
            }
        }
    }
])

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

[
  {
    "_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66",
    "name": "Fabrikam, Inc. | Car Accessory Outlet - West Adele",
    "fullTimeStaff": 3,
    "partTimeStaff": 2,
    "combinedStaffFlag": 3
  }
]

예제 2: 할인율을 사용하는 다중 값 비트 OR

이 쿼리는 특정 프로모션 이벤트에 대한 할인 세부 정보를 추출하고 할인 및 직원 값을 결합한 비트 플래그를 계산합니다. 출력은 이벤트의 Super Saver Spectacular각 할인에 대한 결합된 비트 플래그를 계산하는 집계 쿼리의 결과를 보여 줍니다. 이 작업은 비트 OR: 7|3|2 = 7 및 11|3|2 = 11을 사용하여 할인율을 직원 번호와 결합합니다.

db.stores.aggregate([{
        $match: {
            _id: "f2a8c190-28e4-4e14-9d8b-0256e53dca66"
        }
    },
    {
        $unwind: "$promotionEvents"
    },
    {
        $match: {
            "promotionEvents.eventName": "Super Saver Spectacular"
        }
    },
    {
        $project: {
            name: 1,
            eventName: "$promotionEvents.eventName",
            discountFlags: {
                $map: {
                    input: "$promotionEvents.discounts",
                    as: "discount",
                    in: {
                        categoryName: "$$discount.categoryName",
                        discountPercentage: "$$discount.discountPercentage",
                        combinedFlag: {
                            $bitOr: [
                                "$$discount.discountPercentage",
                                "$staff.totalStaff.fullTime",
                                "$staff.totalStaff.partTime"
                            ]
                        }
                    }
                }
            }
        }
    }
])

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

[
  {
    "_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66",
    "name": "Fabrikam, Inc. | Car Accessory Outlet - West Adele",
    "eventName": "Super Saver Spectacular",
    "discountFlags": [
      {
        "categoryName": "Car Chargers",
        "discountPercentage": 7,
        "combinedFlag": 7
      },
      {
        "categoryName": "Dash Cameras",
        "discountPercentage": 11,
        "combinedFlag": 11
      }
    ]
  }
]