$bitXor演算子は、整数値に対してビットごとの排他的 OR (XOR) 演算を実行します。 XOR 演算は、オペランドの対応するビットが異なるビット位置ごとに 1 を返し、同じ場合は 0 を返します。
構文
{
$bitXor: [ <expression1>, <expression2>, ... ]
}
パラメーター
| パラメーター | Description |
|---|---|
expression1, expression2, ... |
整数値に解決される式。 演算子は、これらの値に対して XOR 操作を順番に実行します。 |
例示
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: 基本的な 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 を計算します。 結果のドキュメントは、 Discount Delight Days イベントのすべての割引率のビットごとの XOR 計算を表します。
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" }
}
]