$bitsAnySet

Bu işleç, belirtilen bit konumlarından herhangi birinin olarak ayarlandığı belgeleri seçmek için 1kullanılır. Bit maskesi değerlerini depolayan alanlara sahip belgeleri sorgulamak için kullanışlıdır. Bu işleç, tek bir tamsayıda birden çok boole bayrağını temsil eden alanlarla çalışırken kullanışlı olabilir.

Syntax

{
  <field>: { $bitsAnySet: [ <bit positions> ] }
}

Parameters

Parametre Description
field Sorgulanacak alan.
<bit positions> Herhangi birinin olarak ayarlı 1olup olmadığını denetlemek için bit konumları dizisi.

Examples

Stores koleksiyonundaki bu örnek belgeyi göz önünde bulundurun.

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

Bu storeFeatures alan, çeşitli depolama özelliklerini temsil eden bir bit maskesi tamsayısıdır. Her bit bir özelliğe karşılık gelir:

Bit Value Feature
0 1 In-Store Teslim Alma
1 2 Otopark
2 4 Tekerlekli Sandalye Erişimi
3 8 24 Saat Aç
4 16 Pet-Friendly
5 32 Ücretsiz Wi-Fi
6 64 Tuvaletler
7 128 Ev Teslimi

Örnek 1: Mağaza içi teslim alma veya sürücü teslim alma işlemleri olan mağazaları bulma

Bu sorgu, ev teslimi VEYA ücretsiz Wi-Fi (bit 5 ve 7) sunan depoları alır

db.stores.find({
    storeFeatures: {
        $bitsAnySet: [5, 7]
    }
}, {
    _id: 1,
    name: 1,
    storeFeatures: 1
}).limit(5)

Eşdeğer:

db.stores.find({
        storeFeatures: {
            $bitsAnySet: 160
        }
    }, // 32 + 128
    {
        _id: 1,
        name: 1,
        storeFeatures: 1
    }).limit(5)

Bu sorgu tarafından döndürülen ilk beş sonuç şunlardır:

[
  {
    "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
    "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
    "storeFeatures": 38
  },
  {
    "_id": "44fdb9b9-df83-4492-8f71-b6ef648aa312",
    "name": "Fourth Coffee | Storage Solution Gallery - Port Camilla",
    "storeFeatures": 222
  },
  {
    "_id": "94792a4c-4b03-466b-91f6-821c4a8b2aa4",
    "name": "Fourth Coffee | Eyewear Shop - Lessiemouth",
    "storeFeatures": 225
  },
  {
    "_id": "728c068a-638c-40af-9172-8ccfa7dddb49",
    "name": "Contoso, Ltd. | Book Store - Lake Myron",
    "storeFeatures": 239
  },
  {
    "_id": "e6410bb3-843d-4fa6-8c70-7472925f6d0a",
    "name": "Relecloud | Toy Collection - North Jaylan",
    "storeFeatures": 108
  }
]