对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

$bitsAnySet(查询谓词运算操作符)

$bitsAnySet

$bitsAnySet matches documents where any of the bit positions given by the query are set (i.e. 1) in field.

{ <field>: { $bitsAnySet: <numeric bitmask> } }

{ <field>: { $bitsAnySet: < BinData bitmask> } }

{ <field>: { $bitsAnySet: [ <position1>, <position2>, ... ] } }

The field value must be either numeric or a BinData instance. Otherwise, $bitsAnySet will not match the current document.

数字位掩码

You can provide a numeric bitmask to be matched against the operand field. The bitmask must be a non-negative 64-bit signed integer. Otherwise, $bitsAnySet returns an error.

BinData Bitmask

您也可以使用任意大的 BinData 实例作为位掩码。

位置列表

如果查询位位置列表,每个 <position> 必须是非负整数。位位置从最低有效位 0 开始。例如,十进制数 254 的位数位置如下:
位值
1
1
1
1
1
1
1
0

Position

7

6

5

4

3

2

1

0

系统的字节序取决于机器的架构。 BSON数据中的数字始终存储为小端,如果您的系统是大端,这意味着数字数据会在大端和小端之间转换。

在位测试匹配表达式操作符的上下文中:

BinData values act as bitmasks and are interpreted as though they are arbitrary-length unsigned little-endian numbers. The lowest-addressable byte is always interpreted as the least significant byte. Similarly, the highest-addressable byte in the BinData is always interpreted as the most significant byte.

Queries cannot use indexes for the $bitsAnySet portion of a query, although the other portions of a query can use indexes, if applicable.

$bitsAnySet 不会匹配无法表示为带符号的 64 位整数的数值。如果某个值太大或太小,超出带符号 64 位整数的范围,或者该值包含小数部分,则可能会出现这种情况。

Numbers are sign extended. For example, $bitsAnySet considers bit position 200 to be set for the negative number -5, but bit position 200 to be clear for the positive number +5.

相反,BinData 实例是零扩展的。例如,给定以下文档:

db.collection.insertOne({ x: BinData(0, "ww=="), binaryValueofA: "11000011" })

$bitsAnySet will consider all bits outside of x to be clear.

下面的示例将使用包含以下文档的集合:

db.collection.insertMany([
{ _id: 1, a: 54, binaryValueofA: "00110110" },
{ _id: 2, a: 20, binaryValueofA: "00010100" },
{ _id: 3, a: 20.0, binaryValueofA: "00010100" },
{ _id: 4, a: BinData(0, "Zg=="), binaryValueofA: "01100110" }
])

The following query uses the $bitsAnySet operator to test whether field a has either bit position 1 or bit position 5 set, where the least significant bit is position 0.

db.collection.find( { a: { $bitsAnySet: [ 1, 5 ] } } )

查询匹配以下文档:

{ "_id" : 1, "a" : 54, "binaryValueofA" : "00110110" }
{ "_id" : 4, "a" : BinData(0,"Zg=="), "binaryValueofA" : "01100110" }

The following query uses the $bitsAnySet operator to test whether field a has any bits set at positions 0, 1, and 5 (the binary representation of the bitmask 35 is 00100011).

db.collection.find( { a: { $bitsAnySet: 35 } } )

查询匹配以下文档:

{ "_id" : 1, "a" : 54, "binaryValueofA" : "00110110" }
{ "_id" : 4, "a" : BinData(0,"Zg=="), "binaryValueofA" : "01100110" }

The following query uses the $bitsAnySet operator to test whether field a has any bits set at positions 4, and 5 (the binary representation of BinData(0, "MA==") is 00110000).

db.collection.find( { a: { $bitsAnySet: BinData(0, "MA==") } } )

查询匹配以下文档:

{ "_id" : 1, "a" : 54, "binaryValueofA" : "00110110" }
{ "_id" : 2, "a" : 20, "binaryValueofA" : "00010100" }
{ "_id" : 3, "a" : 20.0, "binaryValueofA" : "00010100" }
{ "_id" : 4, "a" : BinData(0,"Zg=="), "binaryValueofA" : "01100110" }
给本页内容打分

在此页面上