$exists

$exists运算符检索包含指定字段的文档。 即使字段的值为 null,$exists 运算符也会返回包含指定字段的文档的 true 值。 $exists运算符返回文档结构中不包含指定字段的文档的跌落值。

语法

{
  <field>: { $exists: <true or false> }
}

参数

参数 DESCRIPTION
field 要检查是否存在的字段。
true or false true 对于不包含该字段的文档(包括 null 值 false )。

例子

请考虑存储集合中的此示例文档。

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

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

若要查找任何两个具有促销事件的商店,请使用 promotionEvents 数组上的 $exists 运算符运行查询。 然后,仅投影 ID 和 promotionEvents 字段,并将结果限制为结果集中的两个文档。

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

此查询返回以下结果:

  [
    {
        "_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:确认是否存在嵌套字段

若要检索具有全职员工的任何两个存储,请使用嵌套 staff.employeeCount.fullTime 字段上的 $exists 运算符运行查询。 然后,仅投影名称和 ID 字段,并将结果限制为结果集中的两个文档。

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

此查询返回以下结果:

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