$bitOr演算子は、整数値に対してビットごとの OR 演算を実行します。 最初のオペランドの各ビットを、2 番目のオペランドの対応するビットと比較します。 どちらかのビットが 1 の場合、対応する結果のビットは 1 に設定されます。 両方のビットが 0 の場合、対応する結果ビットは 0 に設定されます。
構文
{
$bitOr: [ <expression1>, <expression2>, ... ]
}
パラメーター
| パラメーター | Description |
|---|---|
expression1, expression2, ... |
整数に評価される式。
$bitOr演算子は、指定されたすべての式に対してビットごとの OR 演算を実行します。 |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_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 操作を実行して、アクセス許可フラグを組み合わせます。 3 (バイナリでは 011) と 2 (バイナリでは 010) のビットごとの OR は 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
}
]
}
]