On this page本页内容
$bitsAllSet
$bitsAllSet
matches documents where all of the bit positions given by the query are set (i.e. 匹配字段中设置了查询给出的所有位位置(即1
) in field
.1
)的文档。
{ <field>: { $bitsAllSet: <numeric bitmask> } } |
{ <field>: { $bitsAllSet: < BinData bitmask> } } |
{ <field>: { $bitsAllSet: [ <position1>, <position2>, ... ] } } |
The field
value must be either numeric or a BinData instance. field
值必须是数字或BinData
实例。Otherwise, 否则,$bitsAllSet
will not match the current document.$bitsAllSet
将与当前文档不匹配。
$bitsAllSet
will return an error.$bitsAllSet
将返回错误。BinData
实例作为位掩码。<position>
must be a non-negative integer. <position>
必须是非负整数。0
from the least significant bit. 0
开始。254
would have the following bit positions:254
将具有以下位位置:Bit Value | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 |
---|---|---|---|---|---|---|---|---|
Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Queries cannot use indexes for the 查询不能对查询的$bitsAllSet
portion of a query, although the other portions of a query can use indexes, if applicable.$bitsAllSet
部分使用索引,尽管查询的其他部分可以使用索引(如果适用)。
$bitsAllSet
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位整数,或者它有一个分数分量,则会出现这种情况。
Numbers are sign extended. 数字是符号扩展的。For example, 例如,$bitsAllSet
considers bit position 200
to be set for the negative number -5
, but bit position 200
to be clear for the positive number +5
.$bitsAllSet
认为要为负数-5
设置位位置200
,但要为正数+5
清除位位置200
。
In contrast, BinData instances are zero-extended. 相反,BinData
实例是零扩展的。For example, given the following document:例如,给定以下文档:
db.collection.save({ x: BinData(0, "ww=="), binaryValueofA: "11000011" })
$bitsAllSet
will consider all bits outside of 将认为x
to be clear.x
以外的所有位都是清除的。
The following examples will use a collection with the following documents:以下示例将使用包含以下文档的集合:
db.collection.save({ _id: 1, a: 54, binaryValueofA: "00110110" }) db.collection.save({ _id: 2, a: 20, binaryValueofA: "00010100" }) db.collection.save({ _id: 3, a: 20.0, binaryValueofA: "00010100" }) db.collection.save({ _id: 4, a: BinData(0, "Zg=="), binaryValueofA: "01100110" })
The following query uses the 以下查询使用$bitsAllSet
operator to test whether field a
has bits set at position 1
and position 5
, where the least significant bit is position 0
.$bitsAllSet
运算符测试字段a
是否在位置1
和位置5
设置了位,其中最低有效位为位置0
。
db.collection.find( { a: { $bitsAllSet: [ 1, 5 ] } } )
The query matches the following documents:查询匹配以下文档:
{ "_id" : 1, "a" : 54, "binaryValueofA" : "00110110" } { "_id" : 4, "a" : BinData(0,"Zg=="), "binaryValueofA" : "01100110" }
The following query uses the 以下查询使用$bitsAllSet
operator to test whether field a
has bits set at positions 1
, 4
, and 5
(the binary representation of the bitmask 50
is 00110010
).$bitsAllSet
运算符测试字段a
是否在位置1
、4
和5
处设置了位(位掩码50的二进制表示形式为00110010)。
db.collection.find( { a: { $bitsAllSet: 50 } } )
The query matches the following document:查询匹配以下文档:
{ "_id" : 1, "a" : 54, "binaryValueofA" : "00110110" }
The following query uses the 以下查询使用$bitsAllSet
operator to test whether field a
has bits set at positions 4
and 5
(the binary representation of BinData(0, "MC==")
is 00110000
).$bitsAllSet
运算符测试字段a
是否在位置4
和5
处设置了位(BinData(0, "MC==")
的二进制表示形式为00110000
)。
db.collection.find( { a: { $bitsAllSet: BinData(0, "MC==") } } )
The query matches the following document:查询匹配以下文档:
{ _id: 1, a: 54, binaryValueofA: "00110110" }