# [AggregateCommand](AggregateCommand).arrayElemAt(value: [Expression](/minigame/dev/wxcloud/reference-sdk-api/database/aggregate/Expression)[]): Object

> 支持端：[小程序 2.7.4](/miniprogram/dev/framework/client-lib/version.md), [云函数 0.8.1](/minigame/dev/wxcloud/reference/changelog-server-sdk), [Web](/minigame/dev/wxcloud/reference/changelog-web-sdk)

聚合操作符。返回在指定数组下标的元素。

## 参数

### value: [Expression](/minigame/dev/wxcloud/reference-sdk-api/database/aggregate/Expression)[]

[<array>, <index>]

## 返回值

### Object

## API 说明

语法如下：

```
db.command.aggregate.arrayElemAt([, ])
```

`<array>` 可以是任意解析为数字的表达式。

`<index>` 可以是任意解析为整形的表达式。如果是正数，`arrayElemAt` 返回在 `index` 位置的元素，如果是负数，`arrayElemAt` 返回从数组尾部算起的 `index` 位置的元素。

## 示例代码

假设集合 `exams` 有如下记录：

```json
{ "_id": 1, "scores": [80, 60, 65, 90] }
{ "_id": 2, "scores": [78] }
{ "_id": 3, "scores": [95, 88, 92] }
```

求各个第一次考试的分数和和最后一次的分数：

```js
const $ = db.command.aggregate
db.collection('exams').aggregate()
  .project({
    first: $.arrayElemAt(['$scores', 0]),
    last: $.arrayElemAt(['$scores', -1]),
  })
  .end()
```

返回结果如下：

```json
{ "_id": 1, "first": 80, "last": 90 }
{ "_id": 2, "first": 78, "last": 78 }
{ "_id": 3, "first": 95, "last": 92 }
```
