定義
バージョン6.3の新機能。
構文
The $bitAnd operator has the following syntax:
{ $bitAnd: [ <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(-1)を返します。
オペランドのいずれかが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) } ] )
2 つの整数を含むビット単位の AND
The following aggregation uses the $bitAnd operator in the $project stage:
db.switches.aggregate( [ { $project: { result: { $bitAnd: [ "$a", "$b" ] } } } ])
この操作は次の結果を返します。
[ { _id: 0, result: 0 } { _id: 1, result: 2 } { _id: 2, result: 1 } ]
ANDLong および Integer を使用したビット単位の
The following aggregation uses the $bitAnd operator in the $project stage:
db.switches.aggregate( [ { $project: { result: { $bitAnd: [ "$a", Long("63") ] } } } ])
この操作は次の結果を返します。
[ { _id: 0, result: Long("0") } { _id: 1, result: Long("2") } { _id: 2, result: Long("3") } ]