$bitsAllClear

$bitsAllClear 运算符用于匹配位掩码中指定的所有位位置都为空(即 0)的文档。 在需要根据字段的二进制表示形式中未设置的特定位筛选文档时,此运算符非常有用。

语法

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

参数

参数 说明
field 要对其执行按位作的文档中的字段。
<bitmask> 一种位掩码,其中每个位位置指定字段值中必须为空 (0) 的相应位位置。

例子

请考虑商店集合中的这个示例文档。

{
    "_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 字段是表示各种存储功能的位掩码整数。 每个位对应于一项功能:

Bit 价值 功能 / 特点
0 1 In-Store 取件
1 2 停车
2 4 轮椅通道
3 8 打开 24 小时
4 16 Pet-Friendly
5 32 免费 Wi-Fi
6 64 厕所
7 128 家庭送货

示例 1:查找未打开 24 小时且不允许宠物的商店

此查询检索未打开 24 小时且不允许宠物的存储(位 3 和 4)

db.stores.find({
    storeFeatures: {
        $bitsAllClear: [3, 4]
    }
}, {
    _id: 1,
    name: 1,
    storeFeatures: 1
}).limit(5)

等效:

db.stores.find({
        storeFeatures: {
            $bitsAnySet: 24
        }
    }, // 8 + 16
    {
        _id: 1,
        name: 1,
        storeFeatures: 1
    }).limit(5)

此查询返回的前五个结果为:

[
  {
    "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
    "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
    "storeFeatures": 38
  },
  {
    "_id": "94792a4c-4b03-466b-91f6-821c4a8b2aa4",
    "name": "Fourth Coffee | Eyewear Shop - Lessiemouth",
    "storeFeatures": 225
  },
  {
    "_id": "1a2c387b-bb43-4b14-a6cd-cc05a5dbfbd5",
    "name": "Contoso, Ltd. | Smart Home Device Vault - Port Katarina",
    "storeFeatures": 36
  },
  {
    "_id": "e88f0096-4299-4944-9788-695c40786d97",
    "name": "Adatum Corporation | Handbag Shoppe - Lucienneberg",
    "storeFeatures": 135
  },
  {
    "_id": "bfb213fa-8db8-419f-8e5b-e7096120bad2",
    "name": "First Up Consultants | Beauty Product Shop - Hansenton",
    "storeFeatures": 135
  }
]