$bitsAnyClear
$bitsAnyClear
$bitsAnyClear
matches documents where any of the bit positions given by the query are clear (i.e.0
) infield
.{ <field>: { $bitsAnyClear: <numeric bitmask> } }
{ <field>: { $bitsAnyClear: <
BinData
bitmask> } }
{ <field>: { $bitsAnyClear: [ <position1>, <position2>, ... ] } }
field
值必须是数字或BinData
实例。否则,$bitsAnyClear
与当前文档不匹配。- 数字位掩码
- 您可以提供一个要与操作数字段匹配的数字位掩码。位掩码必须是非负 64 位有符号整数。否则,
$bitsAnyClear
会返回错误。 - BinData Bitmask
- 您也可以使用任意大的
BinData
实例作为位掩码。 - 位置列表
- 如果查询位位置列表,每个
<position>
必须是非负整数。位位置从最低有效位0
开始。例如,十进制数254
的位数位置如下:
位值11111110Position
7
6
5
4
3
2
1
0
行为
系统的字节序取决于机器的架构。 BSON数据中的数字始终存储为小端,如果您的系统是大端,这意味着数字数据会在大端和小端之间转换。
在位测试匹配表达式操作符的上下文中:
BinData
值充当 位掩码 和 被解释为任意长度的无符号小端数。最低可寻址字节始终被解释为最低有效字节。 同样, BinData
中的最高可寻址字节始终被解释为最高有效字节。
索引
查询不能对查询的 $bitsAnyClear
部分使用索引,但查询的其他部分可以使用索引(如果适用)。
浮点值
$bitsAnyClear
将不匹配无法表示为有符号64位整数的数值。 如果某个值太大或太小而不适合用有符号64位整数表示,或者该值包含小数部分,则可能会出现这种情况。
符号扩展
数字使用符号进行扩展。例如,$bitsAnyClear
认为,对于负数 -5
,位位置 200
已设置,而对于正数 +5
,位位置 200
已清除。
相反,BinData
实例是零扩展的。例如,给定以下文档:
db.collection.insertOne({ x: BinData(0, "ww=="), binaryValueofA: "11000011" })
$bitsAnyClear
会认为 x
之外的所有位都已清除。
示例
下面的示例将使用包含以下文档的集合:
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 $bitsAnyClear
operator to test
whether field a
has either bit position 1
or bit position 5
clear,
where the least significant bit is position 0
.
db.collection.find( { a: { $bitsAnyClear: [ 1, 5 ] } } )
查询匹配以下文档:
{ "_id" : 2, "a" : 20, "binaryValueofA" : "00010100" } { "_id" : 3, "a" : 20.0, "binaryValueofA" : "00010100" }
Integer Bitmask
The following query uses the $bitsAnyClear
operator to test
whether field a
has any bits clear at positions 0
, 1
, and 5
(the binary representation of the bitmask 35
is 00100011
).
db.collection.find( { a: { $bitsAnyClear: 35 } } )
查询匹配以下文档:
{ "_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" }
BinData Bitmask
The following query uses the $bitsAnyClear
operator to test
whether field a
has any bits clear at positions 4
and 5
(the binary representation of BinData(0, "MA==")
is 00110000
).
db.collection.find( { a: { $bitsAnyClear: BinData(0, "MA==") } } )
查询匹配以下文档:
{ "_id" : 2, "a" : 20, "binaryValueofA" : "00010100" } { "_id" : 3, "a" : 20.0, "binaryValueofA" : "00010100" } { "_id" : 4, "a" : BinData(0,"Zg=="), "binaryValueofA" : "01100110" }