The $reverseArray operator flips the order of elements in an array inside MongoDB aggregation pipelines. Use it to show newest-first timelines, reorder tags, or prepare data for downstream stages like $slice.
01
Flip Order
Last becomes first.
02
One Argument
Any array expression.
03
New Array
Does not mutate source.
04
Aggregation
$project / $addFields.
05
Use Cases
Timelines, stacks.
06
vs $sortArray
Reverse vs sort.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $reverseArray operator accepts an array and returns a new array with elements in reverse order. For example, reversing ["a", "b", "c"] gives ["c", "b", "a"].
This is useful when data is stored oldest-first but you want to display newest-first, or when you need the last items in a sequence without changing how they were saved. The original document field is not modified unless you explicitly write the result back.
💡
Beginner Tip
$reverseArray flips position only — it does not sort by value. To order numbers or strings by their values, use $sortArray instead.
Foundation
📝 Syntax
The $reverseArray operator takes a single array expression:
Element at index 0 moves to the end, and the last element moves to index 0. The original tags field in the document is unchanged; only tagsReversed is added in the output.
📈 Practical Patterns
Newest-first feeds, last-N items, and nested pipeline expressions.
Projecting tags with $reverseArray replaces the field value in the aggregation result. To persist this change, use $merge or an update based on the pipeline output.
Bonus — Reverse Leaderboard Order
Flip a pre-sorted scores array for bottom-to-top display:
$reverseArray does not sort — it assumes scores is already ordered high-to-low and simply inverts that order.
Applications
🚀 Use Cases
UI display — show newest-first timelines, comments, or notifications.
Stack simulation — treat a FIFO list as LIFO for processing logic.
Last-N extraction — combine with $slice after reversing.
Data transformation — reorder arrays in ETL without application-side code.
🧠 How $reverseArray Works
1
MongoDB evaluates the array expression
The pipeline resolves the argument to an array value, field path, or nested expression result.
Input
2
Indexes are swapped end to end
Element at index 0 moves to the last position; the last moves to index 0, and so on.
Reverse
3
A new array is returned
The output is a fresh array. The source document field is not modified by the operator itself.
Output
=
🔄
Reversed element order
Use in projections, or chain with $slice and $map.
Wrap Up
Conclusion
The $reverseArray operator returns a new array with elements in reverse index order. It is a simple, fast way to flip timelines, stacks, or any ordered list inside aggregation pipelines.
Remember: it reverses position, not value. For value-based ordering, use $sortArray. Next in the series: $round.
Use $reverseArray for index-order flips (newest-first feeds)
Combine with $slice to get the last N original items
Handle null input with $ifNull when the field may be missing
Use $sortArray when you need value-based ordering
Keep original and reversed fields side by side when debugging
❌ Don’t
Expect $reverseArray to sort numbers or strings by value
Assume nested arrays are reversed internally (only top level flips)
Forget that aggregation output does not auto-update stored documents
Reverse huge arrays repeatedly without considering pipeline cost
Confuse index reversal with descending sort
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $reverseArray
Use these points when reversing arrays in pipelines.
5
Core concepts
🔄01
Flip Order
Last → first.
Purpose
📝02
One argument
Array expression.
Syntax
🛠03
New array
Non-destructive.
Output
🗃04
+ $slice
Last N items.
Chaining
⚠05
[] / null
Empty or missing.
Edge case
❓ Frequently Asked Questions
$reverseArray returns a new array with the elements of the input array in reverse order. The first element becomes last, and the last becomes first. It does not modify the original array field in the document.
The syntax is { $reverseArray: <array expression> }. Use inside $project, $addFields, or $set. The argument can be a field path like "$tags" or any expression that evaluates to an array.
No. Aggregation operators produce new values in the pipeline output. The source document's array field stays unchanged unless you write the reversed result back with $set or a similar update outside the pipeline.
If the input resolves to null or is missing, $reverseArray returns null. An empty array [] reverses to an empty array.
$reverseArray only flips the existing order — it does not sort by value. To sort ascending or descending by a field, use $sortArray instead.