$arrayElemAt(配列の特定の位置にある要素を取得する関数)

$arrayElemAt演算子は、指定した配列インデックスの要素を返すために使用されます。 この演算子は、ドキュメント内の配列から特定の要素を抽出する必要がある場合に役立ちます。

構文

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

パラメーター

パラメーター Description
<array> 要素の取得元の配列参照。
<idx> 返される要素のインデックス。 インデックスは 0 から始まります。 負のインデックスは、配列の末尾からカウントされます。

例示

stores コレクションのこのサンプル ドキュメントについて考えてみましょう。

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