View a markdown version of this page

$bitNot - Amazon DocumentDB

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$bitNot

8.0.1 版中的新增内容。

Amazon DocumentDB 中的运$bitNot算符对整数值或长值执行按位非运算,返回按位补码。对于给定的整数或长整数 n,结果为-(n+1)。

参数

  • expression:解析为整数或长整数的表达式。返回按位补码,对于给定整数或长整数 n,该补码为-(n+1)。

示例(MongoDB Shell)

以下示例显示如何使用$bitNot运算符计算整数值的按位补码。

创建示例文档

db.numbers.insertMany([ {_id: 1, value: 0}, {_id: 2, value: 5}, {_id: 3, value: -3} ]);

查询示例

db.numbers.aggregate([ { $project: { result: { $bitNot: "$value" } } } ]);

输出

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

二进制:不是 0 = -1;不是 5 = -6;不是 -3 = 2。

代码示例

要查看使用$bitNot运算符的代码示例,请选择要使用的语言的选项卡:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); try { await client.connect(); const db = client.db('test'); const collection = db.collection('numbers'); const result = await collection.aggregate([ { $project: { result: { $bitNot: "$value" } } } ]).toArray(); console.log(result); } finally { await client.close(); } } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') try: db = client['test'] collection = db['numbers'] result = list(collection.aggregate([ {'$project': {'result': {'$bitNot': '$value'}}} ])) print(result) finally: client.close() example()