$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:按位或具有折扣百分比的多个值

此查询提取特定促销事件的折扣详细信息,并计算组合折扣和员工值的按位标志。 输出显示聚合查询的结果,该查询计算事件 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
      }
    ]
  }
]