$bitsAnySet$bitsAnySetmatches documents where any of the bit positions given by the query are set (i.e.1) infield.{ <field>: { $bitsAnySet: <numeric bitmask> } }{ <field>: { $bitsAnySet: <BinDatabitmask> } }{ <field>: { $bitsAnySet: [ <position1>, <position2>, ... ] } }The
fieldvalue must be either numeric or aBinDatainstance. Otherwise,$bitsAnySetwill not match the current document.- Máscara de bits numérica
- 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,
$bitsAnySetreturns an error. - Máscara de bits BinData
- Você também pode usar uma instância de
BinDataarbitrariamente grande como uma máscara de bits. - Lista de posições
- Ao consultar uma lista de posições de bits, cada
<position>deve ser um número inteiro não negativo. As posições dos bits começam em0a partir do bit menos significativo. Por exemplo, o número decimal254teria as seguintes posições de bits:
Valor de bits11111110Posição
7
6
5
4
3
2
1
0
Comportamento
A endianidade do seu sistema depende da arquitetura da sua máquina. Os números nos dados BSON são sempre armazenados como little-endian, se o seu sistema for big-endian , isso significa que os dados numéricos são convertidos entre big e little endian.
No contexto dos operadores de expressão de correspondência de teste de bit:
BinData valores agem como bitmasks e são interpretados como se fossem números little-endian sem sinal de comprimento arbitrário. O byte endereçável mais baixo é sempre interpretado como o byte menos significativo. Da mesma forma, o byte endereçável de maior valor no BinData é sempre interpretado como o byte mais significativo.
Indexes
Queries cannot use indexes for the $bitsAnySet portion of a query, although the other portions of a query can use indexes, if applicable.
Valores de ponto flutuante
$bitsAnySet não corresponderá a valores numéricos que não possam ser representados como um número inteiro de 64 bits assinado. Esse pode ser o caso se um valor for muito grande ou muito pequeno para caber em um inteiro de 64 bits assinado ou se tiver um componente fracionário.
Extensão de sinal
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.
Por outro lado, instâncias BinData são estendidas a zero. Por exemplo, dado o seguinte documento:
db.collection.insertOne({ x: BinData(0, "ww=="), binaryValueofA: "11000011" })
$bitsAnySet will consider all bits outside of x to be clear.
Exemplos
Os exemplos a seguir utilizarão uma coleção com os seguintes documentos:
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" } ])
Array de posição de bits
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 ] } } )
A consulta corresponde aos seguintes documentos:
{ "_id" : 1, "a" : 54, "binaryValueofA" : "00110110" } { "_id" : 4, "a" : BinData(0,"Zg=="), "binaryValueofA" : "01100110" }
Máscara de bits inteiros
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 } } )
A consulta corresponde aos seguintes documentos:
{ "_id" : 1, "a" : 54, "binaryValueofA" : "00110110" } { "_id" : 4, "a" : BinData(0,"Zg=="), "binaryValueofA" : "01100110" }
Máscara de bits BinData
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==") } } )
A consulta corresponde aos seguintes documentos:
{ "_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" }