$arrayElemAt

运算符 $arrayElemAt 用于返回指定数组索引处的元素。 当需要从文档中的数组中提取特定元素时,此运算符非常有用。

Syntax

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

参数

参数 Description
<array> 从中检索元素的数组引用。
<idx> 要返回的元素的索引。 该索引从零开始。 从数组末尾开始的负索引计数。

例子

请考虑商店集合中的这个示例文档。

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