> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $reverseArray  (expression operator)

## Definition

Accepts an array expression as an argument and returns an array with the elements in reverse order.

[`$reverseArray`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/reversearray.md#mongodb-expression-exp.-reverseArray) has the following [operator expression syntax:](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-agg-quick-ref-operator-expressions)

```javascript
{ $reverseArray: <array expression> }
```

The argument can be any valid [expression](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) as long as it resolves to an array.

## Behavior

If  the argument resolves to a value of `null` or refers to a missing field, [`$reverseArray`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/reversearray.md#mongodb-expression-exp.-reverseArray) returns `null`.

If the argument does not resolve to an array or `null` nor refers to a missing field, [`$reverseArray`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/reversearray.md#mongodb-expression-exp.-reverseArray) returns an error.

`$reverseArray` returns an empty array when the argument is an empty array.

If the argument contains subarrays, `$reverseArray` only operates on the top level array elements and will not reverse the contents of subarrays.

| Example | Results |
| --- | --- |
| `{ $reverseArray: { $literal: [ 1, 2, 3 ] } }` | `[ 3, 2, 1 ]` |
| `{ $reverseArray:
   { $slice:  [ [ "foo", "bar", "baz", "qux" ], 1, 2 ] } }
}` | `[ "baz", "bar" ]` |
| `{ $reverseArray: null }` | `null` |
| `{ $reverseArray: { $literal: [ ] } }` | `[ ]` |
| `{ $reverseArray: { $literal: [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] } }` | `[ [ 4, 5, 6 ], [ 1, 2, 3 ] ]` |

The examples in the table take a literal argument. To avoid parsing ambiguity if the literal argument is an array, you must wrap the literal array in a [`$literal`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/literal.md#mongodb-expression-exp.-literal) expression or keep the outer array that designates the argument list (e.g. `[ [ 1, 2, 3 ]
]` ) to pass in the literal array `[1, 2, 3]`.

## Example

A collection named `users` contains the following documents:

```javascript
db.users.insertMany( [
   { _id: 1, name: "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] },
   { _id: 2, name: "li", favorites: [ "apples", "pudding", "pie" ] },
   { _id: 3, name: "ahn", favorites: [ ] },
   { _id: 4, name: "ty" }
] )
```

The following example returns an array containing the elements of the `favorites` array in reverse order:

```javascript
db.users.aggregate([
   {
     $project:
      {
         name: 1,
         reverseFavorites: { $reverseArray: "$favorites" }
      }
   }
])
```

The operation returns the following results:

```javascript
[
   { _id: 1, name: "dave123", reverseFavorites: [ "apples", "butter", "cake", "chocolate" ] },
   { _id: 2, name: "li", reverseFavorites: [ "pie", "pudding", "apples" ] },
   { _id: 3, name: "ahn", reverseFavorites: [ ] },
   { _id: 4, name: "ty", reverseFavorites: null },
]
```
