$getField

$getField 연산자는 문서에서 지정된 필드의 값을 검색하는 데 사용됩니다. 동적 필드 이름을 사용하거나 집계 파이프라인 내에서 프로그래밍 방식으로 필드에 액세스해야 하는 경우에 유용합니다.

문법

{
  $getField: {
    field: <string>,
    input: <document>
  }
}

또는 약식 구문:

{
  $getField: <string>
}

매개 변수

매개 변수 Description
field 검색할 필드의 이름을 나타내는 문자열입니다.
input 필드가 검색되는 문서입니다. 지정하지 않으면 기본값은 현재 문서($$ROOT)입니다.

예시

스토어 컬렉션에서 이 샘플 문서를 고려합니다.

{
    "_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: 기본 필드 검색

다음을 사용하여 $getField총 판매액을 검색합니다.

db.stores.aggregate([
  { $match: {"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f"} },
  {
    $project: {
      name: 1,
      totalSalesValue: {
        $getField: {
          field: "totalSales",
          input: "$sales"
        }
      }
    }
  }
])

이 쿼리는 다음 결과를 반환합니다.

[
  {
    "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
    "name": "First Up Consultants | Bed and Bath Center - South Amir",
    "totalSalesValue": 37701
  }
]

예제 2: 약식 구문

약식 구문을 사용하여 저장소 이름을 검색합니다.

db.stores.aggregate([
  { $match: {"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f"} },
  {
    $project: {
      storeName: { $getField: "name" },
      storeLocation: { $getField: "location" }
    }
  }
])

이 쿼리는 다음 결과를 반환합니다.

[
  {
    "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
    "storeName": "First Up Consultants | Bed and Bath Center - South Amir",
    "storeLocation": {
      "lat": 60.7954,
      "lon": -142.0012
    }
  }
]