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

$bitNot(表达式操作符)

6.3版本新增。

$bitNot

返回对单个int或long值执行按位not操作的结果。

$bitNot操作符的语法如下:

{ $bitNot: <expression> }

表达式可以是单个参数,也可以是具有一个int或long元素的数组。

注意

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 将返回错误。

如果表达式的计算结果为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) }
] )

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

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

操作返回以下结果:

[
{ _id: 0, result: -1 },
{ _id: 1, result: -3 },
{ _id: 2, result: -4 }
]
给本页内容打分