文章目录
- 语法
- 用法
- 例子
$bitNot
聚合运算符返回对单个整数或长整数按位进行非运算的结果。
**注意:**从版本6.3才开始支持
语法
{ $bitNot: <expression> }
用法
- 如果参数是其他数据类型,如:字符串、浮点数、小数等返回错误。
- 如果参数表达式为
null
,则返回null
。
例子
使用下面的命令创建switches
集合:
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) }
] )
下面的聚合在$project
阶段使用$bitNot
操作:
db.switches.aggregate( [{$project: {result: {$bitNot: "$a"}}}
])
返回结果如下:
[{ "_id": 0, "result": -1 },{ "_id": 1, "result": -3 },{ "_id": 2, "result": -4 }]