MongoDB $arrayElemAt Operator
MongoDB $arrayElemAt operator is a key component of the aggregation framework, allowing users to retrieve specific elements from arrays within their documents.
In this article, We will learn about the MongoDB $arrayElemAt by understanding various examples in detail and so on.
MongoDB $arrayElemAt
MongoDB provides different types of array expression operators that are used in the aggregation pipeline stages and the $arrayElemAt operator is one of them.
This operator is used to return the element present on the specified index of the given array.
Syntax:
{ $arrayElemAt: [ <array>, <index> ] }
Here, the array must be a valid expression until it resolves to an array and the index must be a valid expression until it resolves to an integer.
- This operator returns the element at the specified positive index position, counting from the start of the array.
- If the value of index is negative then this operator will return the element at the index position, from the end of the array.
- If the index exceeds the array bounds then this operator does not return any result.
Examples of MongoDB $arrayElemAt
To understand the MongoDB $arrayElemAt we will consider below collection called arrayExample and performs various queries on it for better understanding.
Output:

Example 1: Retrieving First and Last Items
In this example, we are going to find the elements of the array(i.e., the value of fruits field) on the specified index using $arrayElemAt operator.
db.arrayExample.aggregate([
{ $match: { name: "Bongo" } },
{ $project: {
firstItem: { $arrayElemAt: ["$fruits", 0] },
lastItem: { $arrayElemAt: ["$fruits", -1] }
}
}
])
Output:

Example 2: Using $arrayElemAt Operator in the Embedded Document
In this example, we are going to find the elements of the array(i.e., the value of favGame.outdoorGames field) on the specified index using $arrayElemAt operator.
db.arrayExample.aggregate([
... {$match: {name: "Piku"}},
... {$project: {
... firstItem: {$arrayElemAt: ["$favGame.outdoorGames", 0]},
... item: {$arrayElemAt: ["$favGame.outdoorGames", 2]}}}])
Output:
Conclusion
The $arrayElemAt operator in MongoDB’s aggregation framework is a handy tool for accessing elements within arrays in your documents. Whether you need to retrieve the first, last, or any other element at a specific index, this operator provides a flexible and efficient way to work with array data.
FAQs on MongoDB $arrayElemAt
Can $arrayElemAt be used with arrays of nested documents?
Yes,
$arrayElemAtcan be used with arrays of nested documents. You can specify the path to the nested array field to access elements within it.
What happens if the index provided to $arrayElemAt is out of bounds?
If the index provided to
$arrayElemAtis out of bounds (i.e., less than 0 or greater than or equal to the array length), the operator will returnnull.
Can $arrayElemAt be used to update array elements?
No,
$arrayElemAtis a read-only operator and cannot be used to update array elements. It is used only for retrieving elements from an array.
Is $arrayElemAt efficient for large arrays?
$arrayElemAtis efficient for accessing elements in large arrays, as MongoDB’s aggregation framework is optimized for such operations. However, frequent use of this operator in complex queries can impact performance, so it’s essential to use it judiciously.


