Last Updated : 15 Jul, 2025
MongoDB is a widely used NoSQL database known for its flexible data model. One of the essential features of MongoDB is its support for array data types. The $arrayElemAt
operator is a key part of MongoDB's aggregation framework, enabling developers to retrieve specific elements from arrays. Whether we need to access the first, last, or an arbitrary element, $arrayElemAt
provides an efficient way to manipulate array data in MongoDB.
The $arrayElemAt
operator allows us to retrieve an element from an array at a specified index. It is often used within the aggregation pipeline to transform and manipulate data efficiently. The operator is particularly useful when we need to work with large datasets containing arrays and need to extract a specific value without looping over the entire array.
Syntax:
{ $arrayElemAt: [ <array>, <index> ] }
Key Terms
<array>
: An array or an expression that resolves to an array.<index>
: The index of the element to retrieve. It can be a positive or negative integer.$arrayElemAt
will retrieve the element starting from the beginning of the array.$arrayElemAt
counts backward from the end of the array.null
$arrayElemAt
The $arrayElemAt
operator can be applied in various scenarios, especially when we need to retrieve specific data from arrays. Below are some common use cases:
We can easily fetch the first or last item from an array by specifying index 0
or -1
, respectively.
When working with large arrays, we may only need to retrieve an element from a particular position. The $arrayElemAt
operator makes this possible.
MongoDB allows arrays within embedded documents. The $arrayElemAt
operator can be used to retrieve elements from arrays within these embedded structures.
To understand the MongoDB $arrayElemAt we will consider below collection called arrayExample and performs various queries on it for better understanding.
Example 1: Retrieving First and Last ItemsIn 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.
Query:
db.arrayExample.aggregate([
{ $match: { name: "Bongo" } },
{ $project: {
firstItem: { $arrayElemAt: ["$fruits", 0] },
lastItem: { $arrayElemAt: ["$fruits", -1] }
}
}
])
Output:
Explanation:$arrayElemAt
operator is used to extract the first and last elements of the fruits
array.0
is "Apple"
, and the element at index -1
(last element) is "Orange"
.In this example, we are going to find the elements from the outdoorGames
array, which is inside the embedded document favGame
, for a document with name: "Piku"
using $arrayElemAt operator.
Query:
db.arrayExample.aggregate([
... {$match: {name: "Piku"}},
... {$project: {
... firstItem: {$arrayElemAt: ["$favGame.outdoorGames", 0]},
... item: {$arrayElemAt: ["$favGame.outdoorGames", 2]}}}])
Output:
Explanation:Here, $arrayElemAt
is used to extract the first item ("Football"
) and the third item (index 2
, "Cricket"
) from the outdoorGames
array inside the favGame
embedded document.
$arrayElemAt
When working with the $arrayElemAt
operator, it's important to follow best practices to ensure efficiency and correctness:
null
.$size
operator to dynamically calculate the array length if needed.$arrayElemAt
in the Aggregation Pipeline
The operator is most commonly used in the aggregation pipeline stages like $project
, $addFields
, or $group
to transform data.
Combine $arrayElemAt
with other aggregation operators like $ifNull
, $cond
, and $size
to handle edge cases such as missing or empty arrays.
Although $arrayElemAt
is efficient, use it carefully in complex queries to avoid performance degradation, especially with large datasets. Ensure that your queries are optimized and that unnecessary use of the operator does not hinder query performance.
The $arrayElemAt
operator in MongoDB's aggregation framework is a handy tool for accessing elements within arrays in your documents. Whether we 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. By understanding its syntax and common use cases, we can easily incorporate this operator into our MongoDB queries and aggregations to manipulate array data with precision.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4