$arrayElemAt

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Array aggregation expressions

What it does

Returns the element at an index; negative indexes count from the end.

Syntax

{$arrayElemAt: [array, index]}

Use case

Select a boundary element from an ordered array.

$arrayElemAt: practical example
{ $project: { latest: { $arrayElemAt: ["$events", -1] } } }
💡 Explanation Returns the element at an index; negative indexes count from the end. When to use it Select a boundary element from an ordered array.
Key Takeaways
  • Returns the element at an index; negative indexes count from the end.
  • Typical use: Select a boundary element from an ordered array.