MongoDB聚合运算符:$maxN

$maxN聚合运算符用于返回聚合分组中最大的n个元素,如果分组中的元素数量小于n,则返回分组的全部元素。

语法

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

参数说明:

  • input:指定输入表达式,表达式用于对分组中的每个元素计算后,$maxN保留最大的n个值。
  • n:用于指定每个分组中要返回的成员数量,n必须是正整数,可以是常量或者依赖于分组内的_id值。

使用

空和缺失值的处理

  • $maxN会过滤掉空值和缺失值

下面的聚合返回分组中最大的n个文档:

db.aggregate( [
   {
      $documents: [
         { playerId: "PlayerA", gameId: "G1", score: 1 },
         { playerId: "PlayerB", gameId: "G1", score: 2 },
         { playerId: "PlayerC", gameId: "G1", score: 3 },
         { playerId: "PlayerD", gameId: "G1" },
         { playerId: "PlayerE", gameId: "G1", score: null }
      ]
   },
   {
      $group:
      {
         _id: "$gameId",
         maximumThreeScores:
            {
               $maxN:
                  {
                     input: "$score",
                     n: 4
                  }
            }
      }
   }
] )

在这个例子中:

  • $document 创建了一个常量文档,包含了运动员的得分
  • $group 根据gameId对文档进行分组,例子中只有一个gameIdG1
  • PlayerD 的得分缺失,PlayerE的得分为空,这些值都被视为空
  • $maxNinput:"$score"返回一个数组放在maximumThreeScores字段
  • 尽管n=4,但因为只有3个文档有得分,$maxN只返回得分最高的3个
[
   {
   _id: 'G1',
   maximumThreeScores: [ 3, 2, 1 ]
   }
]

m a x N 和 maxN和 maxNtopN

$maxN$topN这两个运算符可以得到相同的结果,通常的:

  • 如果想要在没有排序的文档中取最大的前几个值,使用$maxN会更有优势。
  • 如果需要保证特定的顺序,还是需要用$topN
  • 如果不打算对输出值进行排序,可以使用$maxN

关于窗口功能和聚合表达式的支持

  • $maxN可以被用作累加器
  • $maxN也支持作为聚合表达式
  • $maxN同时也支持作为窗口运算符

关于内存的限制

在聚合管道中使用$maxN时,受100M的限制,如果单个分组唱过这一限制,聚合将报错。

举例

使用下面的脚本创建gamescores集合:

db.gamescores.insertMany([
   { playerId: "PlayerA", gameId: "G1", score: 31 },
   { playerId: "PlayerB", gameId: "G1", score: 33 },
   { playerId: "PlayerC", gameId: "G1", score: 99 },
   { playerId: "PlayerD", gameId: "G1", score: 1 },
   { playerId: "PlayerA", gameId: "G2", score: 10 },
   { playerId: "PlayerB", gameId: "G2", score: 14 },
   { playerId: "PlayerC", gameId: "G2", score: 66 },
   { playerId: "PlayerD", gameId: "G2", score: 80 }
])

找出一个项目中前三名的得分

下面的聚合使用$maxN找出一个项目中前三名的得分

db.gamescores.aggregate( [
   {
      $match : { gameId : "G1" }
   },
   {
      $group:
         {
            _id: "$gameId",
            maxThreeScores:
               {
                  $maxN:
                  {
                     input: ["$score","$playerId"],
                     n:3
                  }
               }
         }
   }
] )

在这个例子中:

  • 使用$match筛选出gameIdG1的项目
  • 使用$group根据gameId进行分组,本例中只有一个分组G1
  • 使用input : ["$score","$playerId"]$maxN指定输入字段
  • 使用$maxN返回G1比赛项目中得分最高的三个元素

结果如下:

[
   {
      _id: 'G1',
      maxThreeScores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ]
   }
]

查找多个比赛项目中得分最高的三个

下面的聚合使用$maxN在所有项目中,查找n个得分最高的

db.gamescores.aggregate( [
   {
      $group:
      {
         _id: "$gameId",
         maxScores:
            {
               $maxN:
                  {
                     input: ["$score","$playerId"],
                     n: 3
                  }
            }
      }
   }
] )

本例中:

  • 使用$group依据gameId进行分组
  • 使用$maxN返回所有项目中得分最高的3个
  • 使用input:["$score","$playerId"]$maxN指定输入字段

返回结果如下:

[
   {
      _id: 'G1',
      maxScores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ]
   },
   {
      _id: 'G2',
      maxScores: [ [ 80, 'PlayerD' ], [ 66, 'PlayerC' ], [ 14, 'PlayerB' ] ]
   }
]

基于$group分组的Key计算n

n的值可以动态指定,在下面的例子中,$cond表达式用于gameId字段

db.gamescores.aggregate([
   {
      $group:
      {
         _id: {"gameId": "$gameId"},
         gamescores:
            {
               $maxN:
                  {
                     input: ["$score","$playerId"],
                     n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } }
                  }
            }
      }
   }
] )

本例中:

  • 使用$group依据gameId进行分组
  • 使用input:["$score","$playerId"]$maxN指定输入字段
  • 如果gameIdG2n为1,否则n为3

操作返回结果:

[
   { _id: { gameId: 'G2' }, gamescores: [ [ 80, 'PlayerD' ] ] },
   {
      _id: { gameId: 'G1' },
      gamescores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ]
   }
]

相关推荐

  1. MongoDB聚合运算符:$maxN

    2024-04-06 10:56:03       24 阅读
  2. MongoDB聚合运算符:$max

    2024-04-06 10:56:03       39 阅读
  3. MongoDB聚合运算符:$maxN(用于数组)

    2024-04-06 10:56:03       28 阅读
  4. MongoDB聚合运算符:$add

    2024-04-06 10:56:03       55 阅读
  5. MongoDB聚合运算符:$arrayToObject

    2024-04-06 10:56:03       52 阅读
  6. MongoDB聚合运算符;$dateToParts

    2024-04-06 10:56:03       53 阅读
  7. MongoDB聚合运算符:$dayOfWeek

    2024-04-06 10:56:03       54 阅读
  8. MongoDB聚合运算符:$dayOfMonth

    2024-04-06 10:56:03       51 阅读
  9. MongoDB聚合运算符;$dateToString

    2024-04-06 10:56:03       48 阅读
  10. MongoDB聚合运算符:$dayOfYear

    2024-04-06 10:56:03       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-06 10:56:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-06 10:56:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-06 10:56:03       82 阅读
  4. Python语言-面向对象

    2024-04-06 10:56:03       91 阅读

热门阅读

  1. 排忧解难:线上问题排查工具箱

    2024-04-06 10:56:03       38 阅读
  2. BitWise-Operation

    2024-04-06 10:56:03       36 阅读
  3. rknn3588 yolov5 学习笔记

    2024-04-06 10:56:03       38 阅读
  4. [C++][特殊类设计][单例模式]详细讲解

    2024-04-06 10:56:03       31 阅读
  5. 2024.3.25力扣每日一题——零钱兑换2

    2024-04-06 10:56:03       39 阅读
  6. 数据大屏:现代数据分析与可视化的重要工具

    2024-04-06 10:56:03       35 阅读
  7. 蓝桥杯算法题:外卖店优先级

    2024-04-06 10:56:03       28 阅读
  8. C. Rudolf and the Ugly String

    2024-04-06 10:56:03       33 阅读
  9. 各种负载均衡技术

    2024-04-06 10:56:03       36 阅读
  10. Web 安全之 SSL 剥离攻击详解

    2024-04-06 10:56:03       30 阅读
  11. 【QT教程】QT6 QML在工业控制系统中的应用

    2024-04-06 10:56:03       30 阅读