$bitsAllSet

$bitsAllSet 运算符用于匹配所有指定的位位置均已设置(即为 1)的文档。 此运算符用于对存储整数值的字段执行位运算。 它可用于需要根据整数字段内设置的特定位来筛选文档的场景。

语法

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

参数

参数 Description
field 要对其执行按位作的文档中的字段。
<bitmask> 一个位掩码,指示必须在字段的值中设置哪些位。

例子

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

{
  "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
  "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
  "location": {
    "lat": -74.0427,
    "lon": 160.8154
  },
  "staff": {
    "employeeCount": {
      "fullTime": 9,
      "partTime": 18
    }
  },
  "sales": {
    "salesByCategory": [
      {
        "categoryName": "Stockings",
        "totalSales": 25731
      }
    ],
    "revenue": 25731
  },
  "promotionEvents": [
    {
      "eventName": "Mega Savings Extravaganza",
      "promotionalDates": {
        "startDate": { "Year": 2023, "Month": 6, "Day": 29 },
        "endDate": { "Year": 2023, "Month": 7, "Day": 7 }
      },
      "discounts": [
        { "categoryName": "Stockings", "discountPercentage": 16 },
        { "categoryName": "Tree Ornaments", "discountPercentage": 8 }
      ]
    },
    {
      "eventName": "Incredible Discount Days",
      "promotionalDates": {
        "startDate": { "Year": 2023, "Month": 9, "Day": 27 },
        "endDate": { "Year": 2023, "Month": 10, "Day": 4 }
      },
      "discounts": [
        { "categoryName": "Stockings", "discountPercentage": 11 },
        { "categoryName": "Holiday Cards", "discountPercentage": 9 }
      ]
    },
    {
      "eventName": "Massive Deal Mania",
      "promotionalDates": {
        "startDate": { "Year": 2023, "Month": 12, "Day": 26 },
        "endDate": { "Year": 2024, "Month": 1, "Day": 2 }
      },
      "discounts": [
        { "categoryName": "Gift Bags", "discountPercentage": 21 },
        { "categoryName": "Bows", "discountPercentage": 19 }
      ]
    },
    {
      "eventName": "Super Saver Soiree",
      "promotionalDates": {
        "startDate": { "Year": 2024, "Month": 3, "Day": 25 },
        "endDate": { "Year": 2024, "Month": 4, "Day": 1 }
      },
      "discounts": [
        { "categoryName": "Tree Ornaments", "discountPercentage": 15 },
        { "categoryName": "Stockings", "discountPercentage": 14 }
      ]
    },
    {
      "eventName": "Fantastic Savings Fiesta",
      "promotionalDates": {
        "startDate": { "Year": 2024, "Month": 6, "Day": 23 },
        "endDate": { "Year": 2024, "Month": 6, "Day": 30 }
      },
      "discounts": [
        { "categoryName": "Stockings", "discountPercentage": 24 },
        { "categoryName": "Gift Wrap", "discountPercentage": 16 }
      ]
    },
    {
      "eventName": "Price Plunge Party",
      "promotionalDates": {
        "startDate": { "Year": 2024, "Month": 9, "Day": 21 },
        "endDate": { "Year": 2024, "Month": 9, "Day": 28 }
      },
      "discounts": [
        { "categoryName": "Holiday Tableware", "discountPercentage": 13 },
        { "categoryName": "Holiday Cards", "discountPercentage": 11 }
      ]
    }
  ],
  "company": "Lakeshore Retail",
  "city": "Marvinfort",
  "storeOpeningDate": { "$date": "2024-10-01T18:24:02.586Z" },
  "lastUpdated": { "$timestamp": { "t": 1730485442, "i": 1 } },
  "storeFeatures": 38
}

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:查找有停车场和厕所的商店

查找 有停车场和厕所的 商店(位 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)

示例输出:

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