$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 字段分组,并计算每个活动名称的出现次数,按计数的降序对结果排序。