$bitAnd (aggregation)
On this page本页内容
Definition定义
New in version 6.3. 6.3版新增。
Syntax语法
The $bitAnd
operator has the following syntax:$bitAnd
运算符具有以下语法:
{ $bitAnd: { [ <expression1>, <expression2>, ... ] }
Behavior行为
If the operands include both integers and long values, MongoDB sign-extends the calculated integer result and returns a long value. Otherwise, if the operands include only integers or only longs, MongoDB returns results with the corresponding value type.如果操作数同时包含整数和长值,MongoDB符号将扩展计算出的整数结果并返回一个长值。否则,如果操作数仅包含整数或仅包含long,MongoDB将返回具有相应值类型的结果。
All numbers in mongosh
are doubles, not integers. mongosh
中的所有数字都是双数,而不是整数。To to specify integers in 要在mongosh
, use the NumberInt()
or the NumberLong()
constructor. mongosh
中指定整数,请使用NumberInt()
或NumberLong()
构造函数。To learn more, see Int32 or Long.要了解更多信息,请参阅Int32或Long。
To learn how your MongoDB driver handles numeric values, refer to your driver's documentation.要了解MongoDB驱动程序如何处理数值,请参阅驱动程序文档。
If any arguments in the array are of a different data type such as a string, double, or decimal, MongoDB returns an error.如果数组中的任何参数都是不同的数据类型,如字符串、双精度或十进制,MongoDB将返回一个错误。
If the argument is an empty array, the operation returns 如果参数是空数组,则该操作将返回NumberInt(-1)
.NumberInt(-1)
。
If any of the operands equate to 如果任何操作数等于null
, the operation returns null
.null
,则该操作将返回null
。
Examples实例
The examples on this page use the 此页上的示例使用switches
collection, which contains the following documents:switchs
集合,该集合包含以下文档:
db.switches.insertMany( [
{ _id: 0, a: NumberInt(0), b: NumberInt(127) },
{ _id: 1, a: NumberInt(2), b: NumberInt(3) },
{ _id: 2, a: NumberInt(3), b: NumberInt(5) }
] )
Bitwise AND
with Two Integers带两个整数的按位AND
AND
with Two IntegersThe following aggregation uses the 以下聚合在$bitAnd
operator in the $project
stage:$project
阶段使用$bitAnd
运算符:
db.switches.aggregate( [
{
$project: {
result: {
$bitAnd: [ "$a", "$b" ]
}
}
}
])
The operation returns the following results:该操作返回以下结果:
[
{ _id: 0, result: 0 }
{ _id: 1, result: 2 }
{ _id: 2, result: 1 }
]
Bitwise AND
with a Long and Integer带长整型的按位AND
AND
with a Long and IntegerThe following aggregation uses the 以下聚合在$bitAnd
operator in the $project
stage:$project
阶段使用$bitAnd
运算符:
db.switches.aggregate( [
{
$project: {
result: {
$bitAnd: [ "$a", NumberLong("63") ]
}
}
}
])
The operation returns the following results:该操作返回以下结果:
[
{ _id: 0, result: Long("0") }
{ _id: 1, result: Long("2") }
{ _id: 2, result: Long("3") }
]