$sortByCount

彙總管線中的 $sortByCount 階段用於按指定的運算式來對文件進行分組,然後按遞減排序來對每個群組中的文件計數進行排序。 $sortByCount 階段有助於快速識別資料集中最常見的值。

語法

{
  $sortByCount: <expression>
}

參數

參數 Description
expression 這是用於對文件進行分組和計數的欄位或計算運算式。

範例

請參考商店集合中的此範例檔。

{
    "_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:依名稱分組促銷活動,並依遞減順序計數出現次數

若要依 eventName 字段分組並計算每個事件名稱的出現次數,請以計數的遞減順序排序結果

db.stores.aggregate([
  { $unwind: "$promotionEvents" },
  { $sortByCount: "$promotionEvents.eventName" }
])

此查詢會傳回下列結果:

[
  { "_id": "Crazy Deal Days", "count": 4239 },
  { "_id": "Markdown Madness", "count": 2967 },
  { "_id": "Bargain Bonanza", "count": 2925 },
  { "_id": "Crazy Discount Days", "count": 2922 },
  { "_id": "Price Smash Spectacular", "count": 2915 },
  { "_id": "Super Saver Spectacular", "count": 2900 },
  { "_id": "Crazy Markdown Madness", "count": 2899 },
  { "_id": "Price Cut Carnival", "count": 2868 },
  { "_id": "Grand Bargain Bash", "count": 2849 },
  { "_id": "Bargain Blitz Bash", "count": 2843 },
  { "_id": "Grand Savings Gala", "count": 2826 },
  { "_id": "Super Saver Fiesta", "count": 1551 },
  { "_id": "Major Deal Days", "count": 1548 },
  { "_id": "Price Slash Carnival", "count": 1535 },
  { "_id": "Super Discount Days", "count": 1533 },
  { "_id": "Big Deal Bonanza", "count": 1533 },
  { "_id": "Incredible Savings Showcase", "count": 1531 },
  { "_id": "Unbeatable Savings Spectacular", "count": 1518 },
  { "_id": "Fantastic Deal Days", "count": 1511 },
  { "_id": "Flash Bargain Frenzy", "count": 1504 }
]

此管線將會:

  1. 使用 $unwind 來從輸入文件中解構 promotionEvents 陣列欄位。
  2. 使用 $sortByCount 來按 eventName 欄位分組,並計算每個活動名稱的出現次數,然後按計數的遞減排序來對結果進行排序。