On this page本页内容
$bitsAnySet¶New in version 3.2.版本3.2中的新功能。
$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.
$bitsAnySet will return an error.<position> must be a non-negative integer. Bit positions start at 0 from the least significant bit. For example, the decimal number 254 would have the following bit positions:| 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 $bitsAnySet portion of a query, although the other portions of a query can use indexes, if applicable.
$bitsAnySet will not match numerical values that cannot be represented as a signed 64-bit integer. 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.
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.
In contrast, BinData instances are zero-extended. For example, given the following document:
$bitsAnySet will consider all bits outside of x to be clear.
The following examples will use a collection with the following documents:
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.
The query matches the following documents:
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).
The query matches the following documents:
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, "MC==") is 00110000).
The query matches the following documents: