$bitsAllClear
On this page本页内容
$bitsAllClear
-
$bitsAllClear
matches documents where all of the bit positions given by the query are clear (i.e.匹配字段中查询给出的所有位位置都为空(即0
) infield
.0
)的文档。{ <field>: { $bitsAllClear: <numeric bitmask> } }
{ <field>: { $bitsAllClear: <
BinData
bitmask> } }
{ <field>: { $bitsAllClear: [ <position1>, <position2>, ... ] } }
Thefield
value must be either numeric or aBinData
instance.field
值必须是数字或BinData
实例。Otherwise,否则,$bitsAllClear
will not match the current document.$bitsAllClear
将与当前文档不匹配。Numeric Bitmask数字位掩码You can provide a numeric bitmask to be matched against the operand field.您可以提供要与操作数字段匹配的数字位掩码。It must be representable as a non-negative 32-bit signed integer.它必须表示为非负的32位有符号整数。Otherwise,否则,$bitsAllClear
will return an error.$bitsAllClear
将返回一个错误。- BinData
Bitmask位掩码 You can also use an arbitrarily large您也可以使用任意大的BinData
instance as a bitmask.BinData
实例作为位掩码。Position List位置列表If querying a list of bit positions, each如果查询位位置列表,则每个<position>
must be a non-negative integer. Bit positions start at0
from the least significant bit. For example, the decimal number254
would have the following bit positions:<position>
都必须是非负整数。位位置从最低有效位的0
开始。例如,十进制数字254
将具有以下比特位置:
Bit Value 1 1 1 1 1 1 1 0 Position 7 6 5 4 3 2 1 0
Behavior行为
Indexes索引
Queries cannot use indexes for the 查询不能对查询的$bitsAllClear
portion of a query, although the other portions of a query can use indexes, if applicable.$bitsAllClear
部分使用索引,尽管查询的其他部分可以使用索引(如果适用)。
Floating Point Values浮点值
$bitsAllClear
will not match numerical values that cannot be represented as a signed 64-bit integer. 将与不能表示为带符号64位整数的数值不匹配。This can be the case if a value is either too large or too small to fit in a signed 64-bit integer, or if it has a fractional component.如果一个值太大或太小,无法放入有符号的64位整数,或者它包含小数成分,则可能会出现这种情况。
Sign Extension标志扩展
Numbers are sign extended. 数字是符号扩展的。For example, 例如,$bitsAllClear
considers bit position 200
to be set for the negative number -5
, but bit position 200
to be clear for the positive number +5
.$bitsAllClear
考虑为负数-5
设置位位置200
,但为正数+5
清除位位置200
。
In contrast, 相反,BinData
instances are zero-extended. BinData
实例是零扩展的。For example, given the following document:例如,给定以下文档:
db.collection.insertOne({ x: BinData(0, "ww=="), binaryValueofA: "11000011" })
$bitsAllClear
will consider all bits outside of 将认为x
to be clear.x
之外的所有位都是清除的。
Examples实例
The following examples will use a collection with the following documents:以下示例将使用包含以下文档的集合:
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" }
])
Bit Position Array位位置数组
The following query uses the 下面的查询使用$bitsAllClear
operator to test whether field a
has bits clear at position 1
and position 5
, where the least significant bit is position 0
.$bitsAllClear
运算符来测试字段a
在位置1
和位置5
是否有位清除,其中最低有效位是位置0
。
db.collection.find( { a: { $bitsAllClear: [ 1, 5 ] } } )
The query matches the following documents:查询与以下文档匹配:
{ "_id" : 2, "a" : 20, "binaryValueofA" : "00010100" }
{ "_id" : 3, "a" : 20, "binaryValueofA" : "00010100" }
Integer Bitmask整数位掩码
The following query uses the 以下查询使用$bitsAllClear
operator to test whether field a
has bits clear at positions 0
, 1
, and 5
(the binary representation of the bitmask 35
is 00100011
).$bitsAllClear
运算符来测试字段a
在位置0
、1
和5
处是否有位清除(位掩码35
的二进制表示为00100011
)。
db.collection.find( { a: { $bitsAllClear: 35 } } )
The query matches the following documents:查询与以下文档匹配:
{ "_id" : 2, "a" : 20, "binaryValueofA" : "00010100" }
{ "_id" : 3, "a" : 20, "binaryValueofA" : "00010100" }
BinData Bitmask位掩码
The following query uses the 以下查询使用$bitsAllClear
operator:$bitsAllClear
运算符:
db.collection.find( { a: { $bitsAllClear: BinData(0, "ID==") } } )
The query:查询:
Specifies指定0
as the first value forBinData
, which indicatesID==
is to be interpreted as binary.0
作为BinData
的第一个值,表示ID==将被解释为二进制。The base-64 value二进制中以64为基数的值ID==
in binary is00100000
, which has1
in position 5.ID==
为00100000
,在位置5
中有1
。Uses使用$bitsAllClear
to return documents where thea
field has a clear bit0
in position 5 of the binary value.$bitsAllClear
返回文档,其中a
字段在二进制值的第5
位有一个清除位0
。
The query returns the following documents:查询返回以下文档:
{ "_id" : 2, "a" : 20, "binaryValueofA" : "00010100" }
{ "_id" : 3, "a" : 20, "binaryValueofA" : "00010100" }