> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $arrayElemAt (expression operator)

## Definition

Returns the element at the specified array index.

## Compatibility

You can use `$arrayElemAt` for deployments hosted in the following environments:

- [MongoDB Atlas](https://www.mongodb.com/docs/atlas): The fully managed service for MongoDB deployments in the cloud

* [MongoDB Enterprise](https://www.mongodb.com/docs/manual/administration/install-enterprise.md#std-label-install-mdb-enterprise): The subscription-based, self-managed version of MongoDB

* [MongoDB Community](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition): The source-available, free-to-use, and self-managed version of MongoDB

## Syntax

[`$arrayElemAt`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayelemat.md#mongodb-expression-exp.-arrayElemAt) has the following syntax:

```javascript
{ $arrayElemAt: [ <array expression>, <idx> ] }
```

The `<array expression>` can be any valid [expression](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) that resolves to an array.

The `<idx>` expression can be any valid [expression](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) that resolves to an integer.

For more information on expressions, see [Expressions.](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions)

## Behavior

- If the `<idx>` expression resolves to zero or a positive integer, [`$arrayElemAt`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayelemat.md#mongodb-expression-exp.-arrayElemAt) returns the element at the `idx` position, counting from the start of the array.

- If the `<idx>` expression resolves to a negative integer, [`$arrayElemAt`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayelemat.md#mongodb-expression-exp.-arrayElemAt) returns the element at the `idx` position, counting from the end of the array.

- If `idx` exceeds the array bounds, [`$arrayElemAt`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayelemat.md#mongodb-expression-exp.-arrayElemAt) does not return a result.

- If the `<array expression>` resolves to an undefined array, [`$arrayElemAt`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayelemat.md#mongodb-expression-exp.-arrayElemAt) returns `null`.

| Example | Results |
| --- | --- |
| `{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] }` | `1` |
| `{ $arrayElemAt: [ [ 1, 2, 3 ], -2 ] }` | `2` |
| `{ $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }` | |
| `{ $arrayElemAt: [ "$undefinedField", 0 ] }` | `null` |

## Example

A collection named `users` contains the following documents:

```javascript
db.users.insertMany( [
   { _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" ] }
] )
```

The following example returns the first and last element in the `favorites` array:

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

The operation returns the following results:

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

## See Also

- [`$slice`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice.md#mongodb-expression-exp.-slice)

- [`$first`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/first.md#mongodb-group-grp.-first)

- [`$last`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/last.md#mongodb-group-grp.-last)

- [Array Operators](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-agg-quick-ref-operator-array)
