A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/mongodb/mongodb-reversearray-operator/ below:

MongoDB $reverseArray Operator - GeeksforGeeks

MongoDB $reverseArray Operator

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.

What is 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:

{ $reverseArray: <array expression> }

Key Term:

Key Features of MongoDB $reverseArray Common Use Cases for MongoDB $reverseArray Examples of MongoDB $reverseArray

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.

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.

Query:

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

Output:

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

Query:

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

Output:

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

Query:

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

Output:

Explanation: Important Points for Using $reverseArray

When using the MongoDB $reverseArray operator, keep in mind the following considerations:

  1. Array Expression: The argument passed to $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.
  2. Empty Arrays: If the input array is empty, the operator will return an empty array, which may be useful in some cases where you want to handle edge cases.
  3. Subarrays: The operator only reverses the top-level elements. If your array contains nested arrays, these subarrays will remain unaffected by the reversal.
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. 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