对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

$bitAnd(表达式操作符)

6.3版本新增。

$bitAnd

返回对 int 或long 值的数组执行按位 and 操作的结果。

The $bitAnd operator has the following syntax:

{ $bitAnd: [ <expression1>, <expression2>, ... ] }

如果操作数同时包含整数和长整型值,MongoDB 将对计算出的整数结果进行符号扩展并返回长整型值。 否则,如果操作数仅包含整数或仅长整型,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 驱动程序如何处理数值,请参阅驱动程序的文档。

如果数组中的任何参数属于不同的数据类型,例如字符串、双精度浮点数或十进制数,MongoDB 将返回错误。

如果参数为空数组,则该操作返回Int32(-1) 。

如果任何操作数等于null ,则操作返回null 。

本页上的示例使用switchescollection,其中包含以下文档:

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) }
] )

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 }
]

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") }
]
给本页内容打分