MongoDB聚合运算符:$minN

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

语法

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

参数说明:

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

使用

空和缺失值的处理

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

下面的聚合返回分组中最小的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",
         minimumThreeScores:
            {
               $minN:
                  {
                     input: "$score",
                     n: 4
                  }
            }
      }
   }
] )

在这个例子中:

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

m i n N 和 minN和 minNbottomN

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

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

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

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

关于内存的限制

在聚合管道中使用$minN时,受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 }
])

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

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

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

在这个例子中:

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

结果如下:

[
   {
      _id: 'G1',
      minScores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ]
   }
]

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

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

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

本例中:

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

返回结果如下:

[
   {
      _id: 'G2',
      minScores: [ [ 10, 'PlayerA' ], [ 14, 'PlayerB' ], [ 66, 'PlayerC' ] ]
   },
   {
      _id: 'G1',
      minScores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ]
   }
]

基于$group分组的Key计算n

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

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

本例中:

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

操作返回结果:

[
   { _id: { gameId: 'G2' }, gamescores: [ [ 10, 'PlayerA' ] ] },
   {
      _id: { gameId: 'G1' },
      gamescores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ]
   }
]

相关推荐

  1. MongoDB聚合运算符:$minN

    2024-04-08 11:46:03       40 阅读
  2. MongoDB聚合运算符:$add

    2024-04-08 11:46:03       55 阅读
  3. MongoDB聚合运算符:$arrayToObject

    2024-04-08 11:46:03       53 阅读
  4. MongoDB聚合运算符;$dateToParts

    2024-04-08 11:46:03       53 阅读
  5. MongoDB聚合运算符:$dayOfWeek

    2024-04-08 11:46:03       55 阅读
  6. MongoDB聚合运算符:$dayOfMonth

    2024-04-08 11:46:03       52 阅读
  7. MongoDB聚合运算符;$dateToString

    2024-04-08 11:46:03       48 阅读
  8. MongoDB聚合运算符:$dayOfYear

    2024-04-08 11:46:03       48 阅读
  9. MongoDB聚合运算符:$denseRank

    2024-04-08 11:46:03       42 阅读
  10. MongoDB聚合运算符:$dateTrunc

    2024-04-08 11:46:03       46 阅读

最近更新

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

    2024-04-08 11:46:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-08 11:46:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-08 11:46:03       87 阅读
  4. Python语言-面向对象

    2024-04-08 11:46:03       96 阅读

热门阅读

  1. SqlServer 全文索引

    2024-04-08 11:46:03       36 阅读
  2. HTML:HTML事件汇总

    2024-04-08 11:46:03       35 阅读
  3. Linux Shell:用户配置文件详解

    2024-04-08 11:46:03       36 阅读
  4. MySQL:表的约束(上)

    2024-04-08 11:46:03       35 阅读
  5. 【软设】知识点速记2

    2024-04-08 11:46:03       35 阅读
  6. ffmpeg处理视频命令

    2024-04-08 11:46:03       29 阅读
  7. jenkins参数化构建

    2024-04-08 11:46:03       39 阅读
  8. MySQL的Seconds_Behind_Master 是如何计算的

    2024-04-08 11:46:03       25 阅读
  9. 【HTTP访问某个域名详细过程】

    2024-04-08 11:46:03       32 阅读