다음을 통해 공유


$bitsAnySet

이 연산자는 지정된 비트 위치 중 어느 것이든 로 설정된 1문서를 선택하는 데 사용됩니다. 비트 마스크 값을 저장하는 필드를 사용하여 문서를 쿼리하는 데 유용합니다. 이 연산자는 단일 정수에서 여러 부울 플래그를 나타내는 필드를 사용할 때 유용할 수 있습니다.

문법

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

매개 변수

매개 변수 Description
field 쿼리할 필드.
<bit positions> 로 설정되어 1있는지 확인할 비트 위치의 배열입니다.

예시

스토어 컬렉션에서 이 샘플 문서를 고려합니다.

{
    "_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 8 24시간 열기
4 16 Pet-Friendly
5 32 무료 Wi-Fi
6 64 화장실
7 128 택배

예제 1: 매장 내 픽업 또는 드라이브 스루가 있는 매장 찾기

이 쿼리는 택배 또는 무료 Wi-Fi(비트 5 및 7)를 제공하는 저장소를 검색합니다.

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

해당:

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

이 쿼리에서 반환된 처음 5개의 결과는 다음과 같습니다.

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