$exists

$exists 연산자는 지정된 필드가 포함된 문서를 검색합니다. $exists 연산자는 필드 값이 null인 경우에도 지정된 필드를 포함하는 문서에 대해 true 값을 반환합니다. $exists 연산자는 문서 구조에 지정된 필드가 포함되지 않은 문서에 대해 fall 값을 반환합니다.

문법

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