$exists(元素查询)

适用对象: MongoDB vCore

运算符 $exists 匹配具有指定字段的文档。 $exists如果是true,则选择包含字段的文档,包括字段值所在的null文档。 当为$existsfalse,查询仅返回不包含字段的文档。

语法

$exists 运算符的语法如下:

{
  <field>: { $exists: <boolean> }
}

参数

DESCRIPTION
field 要检查是否存在的字段。
boolean true 如果匹配包含字段的文档(包括 null 值), false 则匹配不包含该字段的文档。

示例:

让我们了解数据集中示例 JSON 的 stores 用法。

{
  "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
  "name": "Trey Research | Home Office Depot - Lake Freeda",
  "location": { "lat": -48.9752, "lon": -141.6816 },
  "staff": { "employeeCount": { "fullTime": 12, "partTime": 19 } },
  "sales": {
    "salesByCategory": [ { "categoryName": "Desk Lamps", "totalSales": 37978 } ],
    "revenue": 37978
  },
  "promotionEvents": [
    {
      "eventName": "Crazy Deal Days",
      "promotionalDates": {
        "startDate": { "Year": 2023, "Month": 9, "Day": 27 },
        "endDate": { "Year": 2023, "Month": 10, "Day": 4 }
      },
      "discounts": [
        { "categoryName": "Desks", "discountPercentage": 22 },
        { "categoryName": "Filing Cabinets", "discountPercentage": 23 }
      ]
    }
  ],
  "company": "Trey Research",
  "city": "Lake Freeda",
  "storeOpeningDate": "2024-09-26T22:55:25.779Z",
  "lastUpdated": "Timestamp({ t: 1729983325, i: 1 })"
}

示例 1:使用促销事件查找商店

该示例查找定义促销事件的所有商店。

db.stores.find(
  { "promotionEvents": { $exists: true }},
  { "_id": 1, "promotionEvents": { $slice: 1 }}
).limit(2)

无论数组是空还是包含元素,查询都会返回字段存在的所有文档 promotionEvents

  {
    "_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 }
        ]
      }
    ],
    "company": "Lakeshore Retail",
    "city": "Marvinfort",
    "storeOpeningDate": "2024-10-01T18:24:02.586Z",
    "lastUpdated": "2024-10-02T18:24:02.000Z"
  },
  {
    "_id": "923d2228-6a28-4856-ac9d-77c39eaf1800",
    "name": "Lakeshore Retail | Home Decor Hub - Franciscoton",
    "location": { "lat": 61.3945, "lon": -3.6196 },
    "staff": { "employeeCount": { "fullTime": 7, "partTime": 6 } },
    "sales": {
      "salesByCategory": [
        { "categoryName": "Lamps", "totalSales": 19880 },
        { "categoryName": "Rugs", "totalSales": 20055 }
      ],
      "revenue": 39935
    },
    "promotionEvents": [
      {
        "eventName": "Unbeatable Markdown Mania",
        "promotionalDates": {
          "startDate": { "Year": 2024, "Month": 3, "Day": 25 },
          "endDate": { "Year": 2024, "Month": 4, "Day": 1 }
        },
        "discounts": [
          { "categoryName": "Vases", "discountPercentage": 8 },
          { "categoryName": "Lamps", "discountPercentage": 5 }
        ]
      }
    ],
    "company": "Lakeshore Retail",
    "city": "Franciscoton",
    "lastUpdated": "2024-12-02T12:01:46.000Z",
    "storeOpeningDate": "2024-09-03T07:21:46.045Z"
  }

示例 2:检查是否存在嵌套字段

此示例查找具有全职员工计数信息的存储。

db.stores.find(
 { "staff.employeeCount.fullTime": { $exists: true }},
 { "_id": 1, "staff.employeeCount": 1 }).limit(2)

查询返回嵌套字段 staff.employeeCount.fullTime 所在的存储区。

  {
    "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
    "staff": {
      "employeeCount": {
        "fullTime": 9,
        "partTime": 18
      }
    }
  },
  {
    "_id": "923d2228-6a28-4856-ac9d-77c39eaf1800",
    "staff": {
      "employeeCount": {
        "fullTime": 7,
        "partTime": 6
      }
    }
  }