> For the complete documentation index, see [llms.txt](https://docs.mongoing.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mongoing.com/can-kao/yun-suan-fu/aggregation-pipeline-operators/arrayelemat-aggregation.md).

# $arrayElemAt (aggregation)

在本页面

* [定义](#definition)
* [行为](#behavior)
* [例子](#examples)

## 定义

**$arrayElemAt**

*3.2版中的新功能。*

返回指定数组索引处的元素。

`$arrayElemAt` 具有以下语法：

```
{ $anyElement: [ <expression> ] }
```

`<array>`表达式可以是任何有效的表达式，只要它可以解析为数组。

`<idx>`表达式可以是任何有效表达式，只要它可以解析为整数。

* 如果为正，则从数组开始算起`$arrayElemAt`返回该`idx`位置的元素 。
* 如果为负，则从数组末尾算起`$arrayElemAt`返回该`idx`位置处的元素 。

如果`idx`超过了数组界限，`$arrayElemAt`则不返回任何结果。

有关表达式的更多信息，请参见 表达式。

## 行为

有关表达式的更多信息，请参见 表达式。

| 例子                                      | 结果 |
| --------------------------------------- | -- |
| { $arrayElemAt: \[ \[ 1, 2, 3 ], 0 ] }  | 1  |
| { $arrayElemAt: \[ \[ 1, 2, 3 ], -2 ] } | 2  |
| { $arrayElemAt: \[ \[ 1, 2, 3 ], 15 ] } |    |

## 例子

名为的集合`users`包含以下文档：

```
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
```

下面的示例返回`favorites`数组中的第一个和最后一个元素 ：

```
db.users.aggregate([
   {
     $project:
      {
         name: 1,
         first: { $arrayElemAt: [ "$favorites", 0 ] },
         last: { $arrayElemAt: [ "$favorites", -1 ] }
      }
   }
])
```

该操作返回以下结果：

```
{ "_id" : 1, "name" : "dave123", "first" : "chocolate", "last" : "apples" }
{ "_id" : 2, "name" : "li", "first" : "apples", "last" : "pie" }
{ "_id" : 3, "name" : "ahn", "first" : "pears", "last" : "cherries" }
{ "_id" : 4, "name" : "ty", "first" : "ice cream", "last" : "ice cream" }
```

译者：李冠飞

校对：


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.mongoing.com/can-kao/yun-suan-fu/aggregation-pipeline-operators/arrayelemat-aggregation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
