$arrayElemAt (배열 요소 추출)

$arrayElemAt 연산자는 지정된 배열 인덱스에서 요소를 반환하는 데 사용됩니다. 이 연산자는 문서 내의 배열에서 특정 요소를 추출해야 하는 경우에 유용합니다.

문법

{
  $arrayElemAt: ["<array>", <idx>]
}

매개 변수

매개 변수 Description
<array> 요소가 검색되는 배열 참조입니다.
<idx> 반환할 요소의 인덱스입니다. 인덱스는 0부터 시작합니다. 음수 인덱스는 배열의 끝에서 계산됩니다.

예시

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

{
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
    "location": {
        "lat": 60.1441,
        "lon": -141.5012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 2,
            "partTime": 0
        }
    },
    "sales": {
        "salesByCategory": [
            {
                "categoryName": "DJ Cables",
                "totalSales": 1000
            },
            {
                "categoryName": "DJ Headphones",
                "totalSales": 35921
            }
        ],
        "fullSales": 3700
    },
    "promotionEvents": [
        {
            "eventName": "Cyber Monday Event",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 8,
                    "Day": 1
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 8,
                    "Day": 7
                }
            },
            "discounts": [
                {
                    "categoryName": "DJ Speakers",
                    "discountPercentage": 25
                }
            ]
        },
        {
            "eventName": "Black Friday Event",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 8,
                    "Day": 1
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 8,
                    "Day": 7
                }
            },
            "discounts": [
                {
                    "categoryName": "DJ Speakers",
                    "discountPercentage": 25
                }
            ]
        },
        {
            "eventName": "Mega Discount Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 5,
                    "Day": 11
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 5,
                    "Day": 18
                }
            },
            "discounts": [
                {
                    "categoryName": "DJ Lights",
                    "discountPercentage": 20
                }
            ]
        }
    ],
    "tag": [
        "#ShopLocal",
        "#NewArrival",
        "#FashionStore",
        "#SeasonalSale",
        "#FreeShipping",
        "#MembershipDeals"
    ]
}

예제 1: 배열 필드에서 첫 번째 요소 반환

이 쿼리는 검색된 저장소에 대한 배열에서 promotionEvents 첫 번째 이벤트 세부 정보를 검색합니다.

db.stores.aggregate([
  { $match: { name: "Lakeshore Retail | DJ Equipment Stop - Port Cecile" } },
  {
    $project: {
      firstPromotionEvent: { $arrayElemAt: ["$promotionEvents", 0] } 
    }
  }
])

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

[
  {
      "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
      "firstPromotionEvent": {
          "eventName": "Cyber Monday Event",
          "promotionalDates": {
              "startDate": {
                  "Year": 2024,
                  "Month": 8,
                  "Day": 1
              },
              "endDate": {
                  "Year": 2024,
                  "Month": 8,
                  "Day": 7
              }
          },
          "discounts": [
              {
                  "categoryName": "DJ Speakers",
                  "discountPercentage": 25
              }
          ]
      }
  }
]