MongoDB $reverseArray Operator (original) (raw)

Last Updated : 15 Apr, 2026

The $reverseArray operator in MongoDB reverses the order of elements in an array within the aggregation pipeline, making it useful for data processing and presentation when the order of values needs to be flipped.

Syntax

{ $reverseArray: }

Features of MongoDB $reverseArray

Here are some features of $reverseArray:

Examples of MongoDB $reverseArray

In these examples, we will work with a sample collection named arrayExample that contains various fields with arrays.

Screenshot-2026-02-13-143321

Example 1: Using $reverseArray Operator

Reverse the value of the numbers2 field using $reverseArray operator. Here, the value of the numbers2 field is an array.

db.arrayExample.aggregate([
{ $match: { name: "Lorenzo" } },
{
$project: {
revNumbers: { reverseArray:"reverseArray: "reverseArray:"numbers2" }
}
}
])

**Output:

reverse

Example 2: Using $reverseArray Operator on Array of Strings

Reverse the value of the fruits field using $reverseArray operator. Here, the value of the fruits field is an array and the elements of the array of string values (fruit names).

db.arrayExample.aggregate([
{ $match: { name: "Bruno" } },
{
$project: {
revStrings: { reverseArray:"reverseArray: "reverseArray:"fruits" }
}
}
])

**Output:

reverse2

Example 3: Using $reverseArray Operator in the Embedded Document

Reverse the value of the favGame.outdoorGames field using $reverseArray operator. Here, the value of the favGame.outdoorGames field is an array and the elements of the array are strings(i.e. outdoor games names).

db.arrayExample.aggregate([
... {$match: {name: "Pika"}},
... {$project: {
... result: {$reverseArray: "$favGame.outdoorGames"}}}])

**Output:

reverse3

Use Cases for MongoDB $reverseArray