定義
バージョン6.3の新機能。
構文
$bitXor演算子の構文は次のとおりです。
{ $bitXor: [ <expression1>, <expression2>, ... ] }
動作
オペランドに整数と long 値の両方が含まれている場合、MongoDB は計算された整数結果を符号拡張し、long 値を返します。 そうではなく、オペランドに整数のみまたは long のみが含まれている場合、MongoDB は対応する値の型を持つ結果を返します。
注意
All numbers in mongosh are doubles, not integers. To specify integers in mongosh, use the Int32() or the Long() constructor. To learn more, see Int32 or Long.
MongoDBドライバーが数値を処理する方法については、 ドライバーのドキュメント を参照してください。
配列内のいずれかの引数がstring、double、decimal など、異なるデータ型である場合、MongoDB はエラーを返します。
引数が空の配列の場合、操作はInt32(0)を返します。
配列内のいずれかの引数がnullと等しい場合、操作はnullを返します。
例
このページの例では、 switchesコレクションを使用しています。
db.switches.insertMany( [ { _id: 0, a: Int32(0), b: Int32(127) }, { _id: 1, a: Int32(2), b: Int32(3) }, { _id: 2, a: Int32(3), b: Int32(5) } ] )
次の集計では、 $projectステージで$bitXor演算子が使用されています。
db.switches.aggregate( [ { $project: { result: { $bitXor: ["$a", "$b"] } } } ])
この操作は次の結果を返します。
[ { _id: 0, result: 127 }, { _id: 1, result: 1 }, { _id: 2, result: 6 } ]