$reverseArray

$reverseArray 运算符用于反转数组中元素的的顺序。 在需要按相反顺序处理或显示数组元素时,此运算符适用。 其是一个数组表达式运算符,可以在聚合管道中使用。

语法

{
  $reverseArray: <array>
}

参数

参数 说明
<array> 要反转顺序的数组。

例子

请考虑商店集合中的这个示例文档。

{
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
    "location": {
        "lat": 60.1441,
        "lon": -141.5012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 2,
            "partTime": 0
        }
    },
    "sales": {
        "salesByCategory": [
            {
                "categoryName": "DJ Headphones",
                "totalSales": 35921
            }
        ],
        "fullSales": 3700
    },
    "promotionEvents": [
        {
            "eventName": "Bargain Blitz Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 3,
                    "Day": 11
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 2,
                    "Day": 18
                }
            },
            "discounts": [
                {
                    "categoryName": "DJ Turntables",
                    "discountPercentage": 18
                },
                {
                    "categoryName": "DJ Mixers",
                    "discountPercentage": 15
                }
            ]
        },
        {
            "eventName": "Discount Delight Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 5,
                    "Day": 11
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 5,
                    "Day": 18
                }
            }
        }
    ],
    "tag": [
        "#ShopLocal",
        "#FashionStore",
        "#SeasonalSale",
        "#FreeShipping",
        "#MembershipDeals"
    ]
}

示例 1:反转数组的顺序

此查询演示了对数组顺序执行反向作的 promotionEvents 运算符用法。

db.stores.aggregate([
    //filtering to one document
    {
        $match: {
            _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
        }
    },
    {
        $project: {
            _id: 1,
            name: 1,
            promotionEventsReversed: {
                $reverseArray: "$promotionEvents"
            }
        }
    },
    // Include only _id, name, promotionalDates and eventName fields in the output 
    {
        $project: {
            _id: 1,
            name: 1,
            "promotionEventsReversed.promotionalDates": 1,
            "promotionEventsReversed.eventName": 1
        }
    }
])

此查询返回以下结果。

[
    {
        "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
        "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
        "promotionEventsReversed": [
            {
                "eventName": "Discount Delight Days",
                "promotionalDates": {
                    "startDate": {
                        "Year": 2024,
                        "Month": 5,
                        "Day": 11
                    },
                    "endDate": {
                        "Year": 2024,
                        "Month": 5,
                        "Day": 18
                    }
                }
            },
            {
                "eventName": "Bargain Blitz Days",
                "promotionalDates": {
                    "startDate": {
                        "Year": 2024,
                        "Month": 3,
                        "Day": 11
                    },
                    "endDate": {
                        "Year": 2024,
                        "Month": 2,
                        "Day": 18
                    }
                }
            }
        ]
    }
]