The Wayback Machine - https://web.archive.org/web/20250221144012/https://www.geeksforgeeks.org/mongodb-reversearray-operator/
Open In App

MongoDB $reverseArray Operator

Last Updated : 24 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The MongoDB $reverseArray operator is a valuable tool for reversing the order of elements in an array, commonly used for various processing or presentation purposes. In this article, We will learn about the MongoDB $reverseArray Operator by understanding various examples in detail.

MongoDB $reverseArray Operator

  • 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: 

The syntax of the $reverseArray operator is:

{ $reverseArray: <array expression> }

Here, the given arguments must be a valid expression until it resolves to an array.

 Examples of MongoDB $reverseArray

To understand it better, let’s look at some examples of the $reverseArray Operator in MongoDB.

In the following examples, we are working with:

Database: GeeksforGeeks
Collection: arrayExample
Document: three documents that contain the details in the form of field-value pairs.

Output:

demo database and collection

Example 1: Using $reverseArray Operator

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.

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

using $reversearray operator example output

Example 2: Using $reverseArray Operator on Array of Strings

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).

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

using $reversearray operator on array of strings example output

Example 3: Using $reverseArray Operator in the Embedded Document

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).

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

using $reversearray operator in the embedded document example output

KeyTakeAways on MongoDB $reverseArray Operator

  • The MongoDB $reverseArray operator is used in the aggregation pipeline stages to reverse the order of elements in an array.
  • It accepts an array expression as an argument and returns an array with the elements in reverse order.
  • If the argument does not resolve to an array or null, then this operator will give an error.
  • If the entered argument resolves a null value, then this operator will return null.
  • If the entered argument refers to a missing field, then this operator will return null.
  • If the entered argument is an empty array, then this operator will return an empty array.
  • If the entered argument contains subarrays, then this operator will work on the top-level array elements and will not reverse the contents of subarrays.

Conclusion

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.

FAQs on MongoDB $reverseArray

Can the $reverseArray operator reverse the contents of subarrays within an array?

No, the $reverseArray operator works only on the top-level array elements and does not reverse the contents of subarrays.

What happens if the argument to $reverseArray is not an array?

If the argument does not resolve to an array or is null, the $reverseArray operator will return an error.

Can $reverseArray be used in update operations to reverse arrays in documents?

No, $reverseArray is used in the aggregation pipeline and cannot be directly used in update operations to modify documents.

Does the $reverseArray operator modify the original array, or does it return a new array with the elements reversed?

$reverseArray returns a new array with the elements in reverse order, leaving the original array unchanged.


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg