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

$bitAnd(表达式操作符)

6.3版本新增。

$bitAnd

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

$bitAnd 操作符的语法如下:

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

如果操作数同时包含整数和长整型值,MongoDB 将对计算出的整数结果进行符号扩展并返回长整型值。 否则,如果操作数仅包含整数或仅长整型,MongoDB 将返回相应值类型的结果。

注意

mongosh中的所有数字都是双精度浮点数,而不是整数。 要在mongosh中指定整数,请使用Int32()或Long()构造函数。 要学习;了解更多信息,请参阅Int 32或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) }] )

以下聚合在 阶段使用$bitAnd $project操作符:

db.switches.aggregate( [ { $project: { result: { $bitAnd: [ "$a", "$b" ] } } }])

操作返回以下结果:

[ { _id: 0, result: 0 } { _id: 1, result: 2 } { _id: 2, result: 1 } ]

以下聚合在 阶段使用$bitAnd $project操作符:

db.switches.aggregate( [ { $project: { result: { $bitAnd: [ "$a", Long("63") ] } } }])

操作返回以下结果:

[ { _id: 0, result: Long("0") } { _id: 1, result: Long("2") } { _id: 2, result: Long("3") } ]
给本页内容打分