MongoDB聚合运算符:$maxN

文章目录

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

语法

js 复制代码
{
   $maxN:
      {
         input: <expression>,
         n: <expression>
      }
}

参数说明:

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

使用

空和缺失值的处理

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

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

js 复制代码
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个
js 复制代码
[
   {
   _id: 'G1',
   maximumThreeScores: [ 3, 2, 1 ]
   }
]

m a x N 和 maxN和 maxN和topN

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

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

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

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

关于内存的限制

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

举例

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

js 复制代码
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找出一个项目中前三名的得分

js 复制代码
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比赛项目中得分最高的三个元素

结果如下:

js 复制代码
[
   {
      _id: 'G1',
      maxThreeScores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ]
   }
]

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

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

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

本例中:

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

返回结果如下:

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

基于$group分组的Key计算n

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

js 复制代码
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

操作返回结果:

js 复制代码
[
   { _id: { gameId: 'G2' }, gamescores: [ [ 80, 'PlayerD' ] ] },
   {
      _id: { gameId: 'G1' },
      gamescores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ]
   }
]
相关推荐
月光水岸New2 小时前
Ubuntu 中建的mysql数据库使用Navicat for MySQL连接不上
数据库·mysql·ubuntu
狄加山6752 小时前
数据库基础1
数据库
我爱松子鱼2 小时前
mysql之规则优化器RBO
数据库·mysql
chengooooooo2 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
Rverdoser3 小时前
【SQL】多表查询案例
数据库·sql
Galeoto3 小时前
how to export a table in sqlite, and import into another
数据库·sqlite
人间打气筒(Ada)4 小时前
MySQL主从架构
服务器·数据库·mysql
leegong231114 小时前
学习PostgreSQL专家认证
数据库·学习·postgresql
喝醉酒的小白4 小时前
PostgreSQL:更新字段慢
数据库·postgresql
敲敲敲-敲代码4 小时前
【SQL实验】触发器
数据库·笔记·sql