View a markdown version of this page

$bitNot - Amazon DocumentDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

$bitNot

8.0.1 版的新功能。

Amazon DocumentDB 中的$bitNot運算子會對整數或長值執行位元 NOT 操作,並傳回位元補數。對於指定的整數或長 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} ]

在二進位:NOT 0 = -1;NOT 5 = -6;NOT -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()