$arrayElemAt
The `$arrayElemAt` operator is used to return the element at the specified array index. This operator is helpful when you need to extract a specific element from an array within your documents.
Syntax
{ $arrayElemAt: ["<array>", <idx>] }
Parameters
<array>
stringrequiredThe array reference from which the element is retrieved.
<idx>
numberrequiredThe index of the element to return. The index is zero-based. A negative index counts from the end of the array.
Examples
Sample Data
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop",
"sales": {
"salesByCategory": [
{ "categoryName": "DJ Cables", "totalSales": 1000 },
{ "categoryName": "DJ Headphones", "totalSales": 35921 }
]
}
}
Retrieve the first element from an array
Use $arrayElemAt to get the first category from the salesByCategory array.
Query:
db.stores.aggregate([
{ $match: { _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5" } },
{
$project: {
name: 1,
firstCategory: { $arrayElemAt: ["$sales.salesByCategory", 0] }
}
}
])
Output:
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop",
"firstCategory": { "categoryName": "DJ Cables", "totalSales": 1000 }
}
]