$bitsAllSet演算子は、指定されたすべてのビット位置が設定されているドキュメント (つまり、1) を照合するために使用されます。 この演算子は、整数値を格納するフィールドに対してビットごとの操作を実行する場合に便利です。 これは、整数フィールド内で設定されている特定のビットに基づいてドキュメントをフィルター処理する必要があるシナリオで使用できます。
構文
{
<field>: { $bitsAllSet: <bitmask> }
}
パラメーター
| パラメーター | Description |
|---|---|
field |
ビットごとの演算を実行するドキュメント内のフィールド。 |
<bitmask> |
フィールドの値に設定する必要があるビットを示すビットマスク。 |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
storeFeatures フィールドは、さまざまなストア機能を表すビットマスク整数です。 各ビットは、次の機能に対応します。
| ビット | 価値 | 特徴 |
|---|---|---|
| 0 | 1 | In-Store ピックアップ |
| 1 | 2 | 駐車場 |
| 2 | 4 | 車いすアクセス |
| 3 | 八 | 24 時間オープン |
| 4 | 16 | Pet-Friendly |
| 5 | 32 | 無料 Wi-Fi |
| 6 | 64 | トイレ |
| 7 | 128 | 宅配 |
例 1: 駐車場とトイレがある店舗を検索する
このクエリは、 駐車場とトイレ (ビット 1 と 6) がある店舗を取得します。
db.stores.find({
storeFeatures: {
$bitsAllSet: [1, 6]
}
}, {
_id: 1,
name: 1,
storeFeatures: 1
}).limit(5)
同等:
db.stores.find({
storeFeatures: {
$bitsAllSet: 66
}
}, {
_id: 1,
name: 1,
storeFeatures: 1
}).limit(5)
このクエリによって返される最初の 5 つの結果は次のとおりです。
[
{
"_id": "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
"name": "Contoso, Ltd. | Office Supply Deals - South Shana",
"storeFeatures": 86
},
{
"_id": "44fdb9b9-df83-4492-8f71-b6ef648aa312",
"name": "Fourth Coffee | Storage Solution Gallery - Port Camilla",
"storeFeatures": 222
},
{
"_id": "728c068a-638c-40af-9172-8ccfa7dddb49",
"name": "Contoso, Ltd. | Book Store - Lake Myron",
"storeFeatures": 239
},
{
"_id": "a2b54e5c-36cd-4a73-b547-84e21d91164e",
"name": "Contoso, Ltd. | Baby Products Corner - Port Jerrold",
"storeFeatures": 126
},
{
"_id": "dda2a7d2-6984-40cc-bbea-4cbfbc06d8a3",
"name": "Contoso, Ltd. | Home Improvement Closet - Jaskolskiview",
"storeFeatures": 107
}
]
関連コンテンツ
- MongoDB から Azure DocumentDB に移行するためのオプションを確認します。
- MongoDB との機能の互換性について詳しくは、こちらをご覧ください。