MongoDB聚合运算符:$bitXor
文章目录
- 语法
- 用法
- 举例
$bitXor聚合运算符返回整数或长整数数组元素按位异或的结果。
语法
{ $bitXor: { [ , , ... ] }
用法
- 如果操作数包括整型和长整型值,MongoDB会对计算出的整数结果进行符号扩展,并返回长整型值,否则返回相应值类型的结果。
- 如果参数数组元素是不同的数据类型,比如:字符串、双精度数或小数,将返回错误。
- 如果参数是空数组,则返回NumberInt(0)
- 如果任何操作数为null,则返回null
举例
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阶段中使用$bitXor对两个整数进行按位异或:
db.switches.aggregate( [ { $project: { result: { $bitXor: [ "$a", "$b" ] } } } ])
操作返回下面的结果:
[ { _id: 0, result: 127 }, { _id: 1, result: 1 }, { _id: 2, result: 6 } ]
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。
还没有评论,来说两句吧...