Last Updated : 15 Jul, 2025
MongoDB is a powerful NoSQL database designed for scalability and flexibility. One of the most useful features MongoDB offers is its ability to handle arrays within documents. The MongoDB $reverseArray
operator is part of the aggregation pipeline, allowing developers to easily reverse the order of elements within an array. This can be particularly useful for sorting, data processing, or presentation purposes.
MongoDB $reverseArray Operator is useful when we need to reverse the order of elements in an array for processing or presentation purposes. $reverseArray provides a straightforward way to manipulate array data in MongoDB. $reverseArray takes an array as an input and returns a new array with the elements in reversed order. $reverse array operator provides an easy way to change the order of array elements and reverse it.
Syntax:
{ $reverseArray: <array expression> }
Key Term:
<array expression>
: This represents the array whose order you want to reverse. It can be a field containing an array, an inline array, or any valid array expression.$reverseArray
null
and Missing Fields: If the specified field is missing or resolves to null
, the operator will return null
.$reverseArray
$reverseArray
operator is a perfect fit.$reverseArray
operator can help when MongoDB's default sorting does not fit your needs, and you need a customized order.To help us understand how the MongoDB $reverseArray
operator works in real-world scenarios, let's explore some practical examples. In these examples, we will work with a sample collection named arrayExample
that contains various fields with arrays.
In this example, we are going to reverse the value of the numbers2 field using $reverseArray operator. Here, the value of the numbers2 field is an array and the elements of the array are numbers.
Query:
db.arrayExample.aggregate([
... {$match: {name: "Lolo"}},
... {$project: {
... revNumbers: {$reverseArray: "$numbers2"}}}])
Output:
Explanation:numbers2
array contains [1, 2, 3, 4, 5, 6]
.$reverseArray
operator reverses this array, and the result is [6, 5, 4, 3, 2, 1]
.In this example, we are going to 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 are strings(i.e. fruits names).
Query:
db.arrayExample.aggregate([
... {$match: {name: "Bongo"}},
... {$project: {
... revStrings: {$reverseArray: "$fruits"}}}])
Output:
Explanation:fruits
array is ["Apple", "Banana", "Carrot", "Lettuce"]
.$reverseArray
operator reverses it to ["Lettuce", "Carrot", "Banana", "Apple"]
.In this example, we are going to 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).
Query:
db.arrayExample.aggregate([
... {$match: {name: "Piku"}},
... {$project: {
... result: {$reverseArray: "$favGame.outdoorGames"}}}])
Output:
Explanation:favGame.outdoorGames
array is ["Table Tennis", "Chess", "Uno", "Snooker"]
.$reverseArray
operator, the result is ["Snooker", "Uno", "Chess", "Table Tennis"]
.When using the MongoDB $reverseArray
operator, keep in mind the following considerations:
$reverseArray
must resolve to a valid array. If it is not an array (or resolves to null
or a missing field), the operator will return null
.The MongoDB $reverseArray
operator offers a convenient way to reverse the order of elements in an array, providing flexibility in array manipulation within MongoDB documents. Its straightforward syntax and ability to handle various scenarios make it a valuable tool for developers working with array data in MongoDB. By mastering this operator, we can manipulate and transform array data effectively within MongoDB aggregation pipelines. Whether you're reversing an array of numbers, strings, or nested documents, this operator gives us the flexibility to meet our specific data-processing needs.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4