次の方法で共有


$sortArray

$sortArray演算子は、配列の要素を並べ替えるために使用されます。 この演算子は、特定のフィールドまたは条件に基づいてドキュメント内の配列を並べ替える必要がある場合に便利です。 埋め込みドキュメントの配列または値の単純な配列に適用できます。

構文

{
  $sortArray: {
    input: <arrayExpression>,
    sortBy: <sortSpecification>
  }
}

パラメーター

パラメーター Description
input 並べ替える配列。
sortBy 並べ替え順序を指定します。 対応する並べ替え順序を持つ 1 つのフィールドまたは複数のフィールドを指定できます (昇順の場合は 1、降順の場合は -1)。

例示

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

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

例 1: 埋め込みドキュメントの配列を並べ替える

このクエリは、各ドキュメント内の sales.salesByCategory 配列を totalSalesに基づいて降順に並べ替えます。

db.stores.aggregate([{
    $match: {
        _id: "d3c9df51-41bd-4b4e-a26b-b038d9cf8b45"
    }
}, {
    $project: {
        sortedSalesByCategory: {
            $sortArray: {
                input: "$sales.salesByCategory",
                sortBy: {
                    totalSales: -1
                }
            }
        }
    }
}])

このクエリは、次の結果を返します。

[
    {
        "_id": "d3c9df51-41bd-4b4e-a26b-b038d9cf8b45",
        "sortedSalesByCategory": [
            {
                "categoryName": "DJ Accessories",
                "totalSales": 60000
            },
            {
                "categoryName": "Music Accessories",
                "totalSales": 40000
            },
            {
                "categoryName": "DJ Speakers",
                "totalSales": 36972
            },
            {
                "categoryName": "DJ Headphones",
                "totalSales": 12877
            }
        ]
    }
]

- MongoDB から Azure DocumentDB に移行するためのオプションを確認します。 - MongoDB との機能の互換性の詳細を参照してください。