你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

$maxN

$maxN累加器用于从数组或分组数据中检索前 N 个最大值。 它通常用于聚合管道,以基于数字或可比较字段对数据进行排序和筛选。

语法

$maxN: {
  input: <expression>,
  n: <positive integer>
}

参数

参数 DESCRIPTION
input 指定要计算最大值的字段或表达式。
n 指定要检索的最大值的数目。 必须是正整数。

例子

让我们了解使用此架构构建的文档的用法。

{
  "_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
        }
      ]
    }
  ],
  "tag": [
    "#ShopLocal",
    "#SeasonalSale",
    "#FreeShipping",
    "#MembershipDeals"
  ],
  "company": "Lakeshore Retail",
  "city": "Port Cecile",
  "lastUpdated": {
    "$date": "2024-12-11T10:21:58.274Z"
  }
}

示例 1:基于总销售额检索前 2 个销售类别

以下聚合管道检索排名最高的前 2 个销售类别 totalSales

db.stores.aggregate([
  {
    $project: {
      topSalesCategories: {
        $maxN: {
          input: "$sales.salesByCategory",
          n: 2
        }
      }
    }
  },
  {
	  $limit: 4 
  }
])

此查询将返回以下文档。

[
  {
    _id: 'e6895a31-a5cd-4103-8889-3b95a864e5a6',
    topSalesCategories: [ { categoryName: 'Photo Albums', totalSales: 17676 } ]
  },
  {
    _id: 'b5c9f932-4efa-49fd-86ba-b35624d80d95',
    topSalesCategories: [ { categoryName: 'Rulers', totalSales: 35346 } ]
  },
  {
    _id: '5c882644-f86f-433f-b45e-88e2015825df',
    topSalesCategories: [
      { categoryName: 'iPads', totalSales: 39014 },
      { categoryName: 'Unlocked Phones', totalSales: 49969 }
    ]
  },
  {
    _id: 'cba62761-10f8-4379-9eea-a9006c667927',
    topSalesCategories: [
      { categoryName: 'Ultrabooks', totalSales: 41654 },
      { categoryName: 'Toner Refill Kits', totalSales: 10726 }
    ]
  }
]

示例 2:使用$maxN$setWindowFields

每个城市 2023 年类别“笔记本电脑”的 N 大折扣

db.yourCollection.aggregate([
  { $unwind: "$promotionEvents" },
  { $unwind: "$promotionEvents.discounts" },

  // Match only "Laptops" discounts from year 2023
  {
    $match: {
      "promotionEvents.discounts.categoryName": "Laptops",
      "promotionEvents.promotionalDates.startDate.Year": 2023
    }
  },

  // Group by city and collect top N max discounts
  {
    $group: {
      _id: "$city",
      topDiscounts: {
        $maxN: {
          input: "$promotionEvents.discounts.discountPercentage",
          n: 3  // Change this to however many top discounts you want
        }
      }
    }
  }
])

此查询将返回以下文档。

[
  { _id: 'Lake Margareteland', topDiscounts: [ 18 ] },
  { _id: 'Horacetown', topDiscounts: [ 13 ] },
  { _id: "D'Amoreside", topDiscounts: [ 9 ] },
  { _id: 'North Berylborough', topDiscounts: [ 23, 22 ] },
  { _id: 'Windlerbury', topDiscounts: [ 18 ] },
  { _id: 'New Jalonhaven', topDiscounts: [ 21, 19 ] },
  { _id: 'Quitzonhaven', topDiscounts: [ 23 ] },
  { _id: 'North Eladio', topDiscounts: [ 7 ] },
  { _id: 'Terrenceberg', topDiscounts: [ 15 ] },
  { _id: 'Ruthville', topDiscounts: [ 16 ] },
  { _id: 'Loweshire', topDiscounts: [ 13 ] },
  { _id: 'North Korbin', topDiscounts: [ 15 ] },
  { _id: 'Port Jeraldborough', topDiscounts: [ 15 ] },
  { _id: 'Raystad', topDiscounts: [ 11 ] },
  { _id: 'Port Enola', topDiscounts: [ 14 ] },
  { _id: 'Maverickhaven', topDiscounts: [ 14 ] },
  { _id: 'Lake Avafort', topDiscounts: [ 7 ] },
  { _id: 'Ziemannville', topDiscounts: [ 15 ] },
  { _id: 'South Dahliachester', topDiscounts: [ 5 ] },
  { _id: 'Agnesshire', topDiscounts: [ 14, 8 ] }
]