Database Manual / Reference / Query Language / Expressions

$bitNot (expression operator)(表达式运算符)

Definition定义

New in version 6.3.在版本6.3中新增。

$bitNot
Returns the result of a bitwise not operation on a single int or long value.返回对单个intlong值进行按位非运算的结果。

Syntax语法

The $bitNot operator has the following syntax:$bitNot运算符具有以下语法:

{ $bitNot: <expression> }

The expression can be a single argument or an array with one int or long element.表达式可以是单个参数,也可以是包含一个intlong元素的数组。

Behavior行为

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.要了解更多信息,请参阅Int32Long

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 expression evalutates to null, the operation returns null.如果表达式的值为null,则操作返回null

Example示例

The example on this page uses the switches collection:此页面上的示例使用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) }
] )

The following aggregation uses the $bitNot operator in the $project stage:以下聚合在$project阶段使用$bitNot运算符:

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

The operation returns the following results:该操作返回以下结果:

  [
{ _id: 0, result: -1 },
{ _id: 1, result: -3 },
{ _id: 2, result: -4 }
]

Learn More了解更多