Definition定义
New in version 6.3.在版本6.3中新增。
$bitOrReturns the result of a bitwise返回对oroperation on an array ofintandlongvalues.int和long值数组进行按位OR运算的结果。
Syntax语法
The $bitOr operator has the following syntax:$bitOr运算符具有以下语法:
{ $bitOr: [ <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符号将扩展计算出的整数结果并返回长值。否则,如果操作数仅包含整数或长整型,MongoDB将返回具有相应值类型的结果。
Note
All numbers in mongosh are doubles, not integers. mongosh中的所有数字都是双精度的,而不是整数。To specify integers in 要在mongosh, use the Int32() or the Long() constructor. mongosh中指定整数,请使用Int32()或Long()构造函数。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.如果数组中的任何参数属于不同的数据类型,如字符串、double或decimal,MongoDB将返回错误。
If the argument is an empty array, the operation returns 如果参数是空数组,则操作返回Int32(0).Int32(0)。
If any of the arguments in the array equate to 如果数组中的任何参数等于null, the operation returns null.null,则操作返回null。
Examples示例
The examples on this page use the 此页面上的示例使用switch集合,其中包含以下文档:switches collection, which contains the following documents:
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) }
] )
Bitwise OR with Two Integers具有两个Integer的按位OR
OR with Two IntegersThe following aggregation uses the 以下聚合在$bitOr operator in the $project stage:$project阶段使用$bitOr运算符:
db.switches.aggregate( [
{
$project: {
result: {
$bitOr: [ "$a", "$b" ]
}
}
}
])
The operation returns the following results:该操作返回以下结果:
[
{ _id: 0, result: 127 },
{ _id: 1, result: 3 },
{ _id: 2, result: 7 }
]Bitwise OR with a Long and Integer具有Long和Integer的按位OR
OR with a Long and IntegerThe following aggregation uses the 以下聚合在$bitOr operator in the $project stage:$project阶段使用$bitOr运算符:
db.switches.aggregate( [
{
$project: {
result: {
$bitOr: [ "$a", Long("63") ]
}
}
}
])
The operation returns the following results:该操作返回以下结果:
[
{ _id: 0, result: Long("0") },
{ _id: 1, result: Long("2") },
{ _id: 2, result: Long("3") }
]