$bitsAllSet$bitsAllSetmatches documents where all of the bit positions given by the query are set (i.e.1) infield.{ <field>: { $bitsAllSet: <numeric bitmask> } }{ <field>: { $bitsAllSet: <BinDatabitmask> } }{ <field>: { $bitsAllSet: [ <position1>, <position2>, ... ] } }The
fieldvalue must be either numeric or aBinDatainstance. Otherwise,$bitsAllSetwill not match the current document.- Máscara de bits numérica
- Puede proporcionar una máscara de bits numérica que se comparará con el campo del operando. La máscara de bits debe ser un entero con signo de 0 64bits no negativo. De lo contrario, devuelve un
$bitsAllSeterror. - BinData Bitmask
- También puedes usar una instancia
BinDataarbitrariamente grande como máscara de bits. - Lista de cargos
- Si se consulta una lista de posiciones de bits, cada
<position>debe ser un número entero no negativo. Las posiciones de bits comienzan en0desde el bit menos significativo. Por ejemplo, el número decimal254tendría las siguientes posiciones de bits:
Valor de bit11111110Posición
7
6
5
4
3
2
1
0
Comportamiento
La endianidad de tu sistema depende de la arquitectura de tu equipo. Los números en los datos BSON siempre se almacenan como little-endian, y si tu sistema es big-endian esto significa que los datos numéricos se convierten entre big y little endian.
En el contexto de los operadores de expresiones de coincidencia de prueba de bits:
BinData values act as bitmasks and are interpreted as though they are arbitrary-length unsigned little-endian numbers. The lowest-addressable byte is always interpreted as the least significant byte. Similarly, the highest-addressable byte in the BinData is always interpreted as the most significant byte.
Indexes
Queries cannot use indexes for the $bitsAllSet portion of a query, although the other portions of a query can use indexes, if applicable.
Valores de punto flotante
$bitsAllSet no coincidirán con valores numéricos que no puedan ser representados como un entero de 64 bits con signo. Este puede ser el caso si un valor es demasiado grande o demasiado pequeño para caber en un entero con signo de 64 bits, o si tiene un componente fraccionario.
Extensión de signo
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.
En cambio, las instancias de BinData tienen una extensión cero. Por ejemplo, dado el siguiente documento:
db.collection.insertOne({ x: BinData(0, "ww=="), binaryValueofA: "11000011" })
$bitsAllSet will consider all bits outside of x to be clear.
Ejemplos
Los siguientes ejemplos usarán una colección con los siguientes 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" } ])
arreglo de Posición de Bits
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.
db.collection.find( { a: { $bitsAllSet: [ 1, 5 ] } } )
La query coincide con los siguientes documentos:
{ "_id" : 1, "a" : 54, "binaryValueofA" : "00110110" } { "_id" : 4, "a" : BinData(0,"Zg=="), "binaryValueofA" : "01100110" }
Integer Bitmask
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).
db.collection.find( { a: { $bitsAllSet: 50 } } )
La query coincide con el siguiente documento:
{ "_id" : 1, "a" : 54, "binaryValueofA" : "00110110" }
BinData Bitmask
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, "MA==") is 00110000).
db.collection.find( { a: { $bitsAllSet: BinData(0, "MA==") } } )
La query coincide con el siguiente documento:
{ _id: 1, a: 54, binaryValueofA: "00110110" }