$bitsAnyClear

On this page本页内容

$bitsAnyClear

$bitsAnyClear matches documents where any of the bit positions given by the query are clear (i.e. 0) in field.匹配field中查询给出的任何位位置都是清除(即0)的文档。

{ <field>: { $bitsAnyClear: <numeric bitmask> } }
{ <field>: { $bitsAnyClear: < BinData bitmask> } }
{ <field>: { $bitsAnyClear: [ <position1>, <position2>, ... ] } }

The field value must be either numeric or a BinData instance. field值必须是数字或BinData实例。Otherwise, $bitsAnyClear will not match the current document.否则,$bitsAnyClear将与当前文档不匹配。

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, $bitsAnyClear will return an error.否则,$bitsAnyClear将返回错误。
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. 如果查询位位置列表,则每个<position>必须是非负整数。Bit positions start at 0 from the least significant bit. 位位置从最低有效位的0开始。For example, the decimal number 254 would have the following bit positions:例如,十进制数254将具有以下位位置:
Bit Value11111110
Position76543210

Behavior行为

Indexes索引

Queries cannot use indexes for the $bitsAnyClear portion of a query, although the other portions of a query can use indexes, if applicable.查询不能对查询的$bitsAnyClear部分使用索引,尽管查询的其他部分可以使用索引(如果适用)。

Floating Point Values浮点值

$bitsAnyClear 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, $bitsAnyClear considers bit position 200 to be set for the negative number -5, but bit position 200 to be clear for the positive number +5.例如,$bitsAnyClear认为位位置200是为负数-5设置的,而位位置200则是为正数+5设置的。

In contrast, BinData instances are zero-extended. 相反,BinData实例是零扩展的。For example, given the following document:例如,给定以下文档:

db.collection.save({ x: BinData(0, "ww=="), binaryValueofA: "11000011" })

$bitsAnyClear 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.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" })

Bit Position Array位位置数组

The following query uses the $bitsAnyClear operator to test whether field a has either bit position 1 or bit position 5 clear, where the least significant bit is position 0.以下查询使用$bitsAnyClear运算符测试字段a是否清除了位位置1或位位置5,其中最低有效位为位置0

db.collection.find( { a: { $bitsAnyClear: [ 1, 5 ] } } )

The query matches the following documents:查询匹配以下文档:

{ "_id" : 2, "a" : 20, "binaryValueofA" : "00010100" }
{ "_id" : 3, "a" : 20.0, "binaryValueofA" : "00010100" }

Integer Bitmask整数位掩码

The following query uses the $bitsAnyClear operator to test whether field a has any bits clear at positions 0, 1, and 5(the binary representation of the bitmask 35 is 00100011).以下查询使用$bitsAnyClear运算符测试字段a在位置015处是否有任何位清除(位掩码35的二进制表示形式为00100011)。

db.collection.find( { a: { $bitsAnyClear: 35 } } )

The query matches the following documents:查询匹配以下文档:

{ "_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" }

BinData BitmaskBinData位掩码

The following query uses the $bitsAnyClear operator to test whether field a has any bits clear at positions 4 and 5(the binary representation of BinData(0, "MC==") is 00110000).以下查询使用$bitsAnyClear运算符测试字段a在位置45处是否有任何位清除(BinData(0, "MC==")的二进制表示形式为00110000)。

db.collection.find( { a: { $bitsAnyClear: BinData(0, "MC==") } } )

The query matches the following documents:查询匹配以下文档:

{ "_id" : 2, "a" : 20, "binaryValueofA" : "00010100" }
{ "_id" : 3, "a" : 20.0, "binaryValueofA" : "00010100" }
{ "_id" : 4, "a" : BinData(0,"Zg=="), "binaryValueofA" : "01100110" }
←  $bitsAllSet$bitsAnySet →