$bitsAllClear - Amazon DocumentDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

$bitsAllClear

Amazon DocumentDB 中的$bitsAllClear运算符用于匹配已清除字段中所有指定位(设置为 0)的文档。此运算符可用于对存储的数据执行按位运算。

参数

  • field:用于检查指定位是否被清除的字段。

  • value:指定应检查哪些位的数字位掩码,或要检查的位位置列表。数字位掩码可以是二进制 (0b...)、十进制、十六进制 (0x...)、八进制 (0o...) 或二进制 () 形式。BinData在位位置列表中,最低有效位的位置为 0。

示例(MongoDB 外壳)

以下示例演示了$bitsAllClear运算符在 Amazon DocumentDB 中的用法。

创建示例文档

db.collection.insertMany([ { _id: 1, bits: 0b1010 }, { _id: 2, bits: 0b1100 }, { _id: 3, bits: 0b0101 } ]);

查询示例

db.collection.find({ bits: { $bitsAllClear: 0b0011 } })

输出

{ "_id" : 2, "bits" : 12 }

该查询会检查bits字段中是否清除了位掩码指定的所有位0b0011(两个最低有效位)。带有 _id 2 的文档满足此条件,因为其bits字段已清除这些位。

代码示例

要查看使用该$bitsAllClear命令的代码示例,请选择要使用的语言的选项卡:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('collection'); const result = await collection.find({ bits: { $bitsAllClear: 0b0011 } }).toArray(); console.log(result); 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') db = client['test'] collection = db['collection'] result = list(collection.find({ 'bits': { '$bitsAllClear': 0b0011 } })) print(result) client.close() example()