次の方法で共有


$reverseArray(配列を逆にする)

$reverseArray演算子は、配列内の要素の順序を逆にするために使用されます。 この演算子は、配列要素を逆の順序で処理または表示する必要がある場合に便利です。 これは配列式演算子であり、集計パイプライン内で使用できます。

構文

{
  $reverseArray: <array>
}

パラメーター

パラメーター Description
<array> 反転する配列。

例示

stores コレクションのこのサンプル ドキュメントについて考えてみましょう。

{
    "_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
                    }
                }
            }
        ]
    }
]