AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

$bitsAnySet(クエリ述語演算子)

$bitsAnySet

$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.

数値ビットマスク

オペランドフィールドと照合するための数値ビットマスクを指定できます。ビットマスクは、負でない64 ビットの符号付き整数である必要があります。それ以外の場合、$bitsAnySet はエラーを返します。

BinData Bitmask

任意の大きなBinDataインスタンスをビットマスクとして使用することもできます。

位置リスト

ビット位置のリストをクエリする場合、各<position>は負でない整数である必要があります。 ビット位置は最下位ビットの0から始まります。 たとえば、10 進数254は次のビット位置を持ちます。
ビット値
1
1
1
1
1
1
1
0

位置

7

6

5

4

3

2

1

0

システムのエンディアン性は、マシンのアーキテクチャによって異なります。 BSON データ内の数値は常にリトルエンディアンとして保存されます。システムがビッグエンディアンの場合、数値データはビッグエンディアンとリトルエンディアンの間で変換されます。

ビットテスト マッチ式演算子のコンテキストでは、次のようになります。

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.

Queries cannot use indexes for the $bitsAnySet portion of a query, although the other portions of a query can use indexes, if applicable.

$bitsAnySet は、符号付き 64 ビット整数として表できない数値と一致しません。 これは、値が符号付き 64 ビット整数に収まらないほど大きいか小さい場合、または端数コンポーネントがある場合に当てはまります。

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.

対照的に、 BinDataインスタンスはゼロ拡張です。 たとえば、次のドキュメントがあるとします。

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

$bitsAnySet will consider all bits outside of x to be clear.

次の例では、次のドキュメントを含むコレクションを使用します。

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" }
])

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 ] } } )

クエリは、次のドキュメントと一致します。

{ "_id" : 1, "a" : 54, "binaryValueofA" : "00110110" }
{ "_id" : 4, "a" : BinData(0,"Zg=="), "binaryValueofA" : "01100110" }

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 } } )

クエリは、次のドキュメントと一致します。

{ "_id" : 1, "a" : 54, "binaryValueofA" : "00110110" }
{ "_id" : 4, "a" : BinData(0,"Zg=="), "binaryValueofA" : "01100110" }

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==") } } )

クエリは、次のドキュメントと一致します。

{ "_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" }
このページを評価

項目一覧