다음을 통해 공유


$bitsAllSet

$bitsAllSet 연산자는 지정된 모든 비트 위치가 설정된 문서(즉, 1)를 일치시키는 데 사용됩니다. 이 연산자는 정수 값을 저장하는 필드에 비트 연산을 수행하는 데 유용합니다. 정수 필드 내에서 설정되는 특정 비트를 기반으로 문서를 필터링해야 하는 시나리오에서 사용할 수 있습니다.

문법

{
  <field>: { $bitsAllSet: <bitmask> }
}

매개 변수

매개 변수 Description
field 비트 연산을 수행할 문서의 필드입니다.
<bitmask> 필드 값에 설정해야 하는 비트를 나타내는 비트 마스크입니다.

예시

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

{
    "_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: 주차 및 화장실이 있는 매장 찾기

이 쿼리는 주차 및 화장실이 있는 저장소(비트 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
  }
]