$reverseArray
The $reverseArray operator returns a new array with the elements in reverse order.
📌 Syntax
{ "$reverseArray": <arrayExpression> }
✅ Base Example – Reverse Numbers
📥 Input Document
{ "nums": [1, 2, 3, 4, 5] }
📌 Expression
{ "$reverseArray": "$nums" }
📤 Output
[5, 4, 3, 2, 1]
✅ Base Example – Reverse Strings
📥 Input Document
{ "tags": ["new", "sale", "exclusive"] }
📌 Expression
{ "$reverseArray": "$tags" }
📤 Output
["exclusive", "sale", "new"]
🧱 Ecommerce Example – Show Most Recent Orders First
📌 Pipeline
[
{
"$project": {
"customer": "$name",
"recentOrders": {
"$reverseArray": "$orders"
}
}
}
]
📥 Input Document
{
"name": "Alice",
"orders": [
{ "orderId": 1, "amount": 50 },
{ "orderId": 2, "amount": 75 },
{ "orderId": 3, "amount": 30 }
]
}
📤 Output
{
"customer": "Alice",
"recentOrders": [
{ "orderId": 3, "amount": 30 },
{ "orderId": 2, "amount": 75 },
{ "orderId": 1, "amount": 50 }
]
}
🔧 Common Use Cases
- Display latest entries first
- Change processing order
- Use before slicing or accessing via index
🔗 Related Operators
$slice,$arrayElemAt,$map,$concatArrays
🧠Notes
- Input must be an array; otherwise returns
null. - Does not modify the original array.