MongoDB聚合运算符:$top

MongoDB聚合运算符:$top

文章目录

$top聚合运算符返回一个指定顺序分组的第一个元素。

语法

js 复制代码
{
   $top:
      {
         sortBy: { <field1>: <sort order>, <field2>: <sort order> ... },
         output: <expression>
      }
}

字段说明:

字段 是否必须 描述
sortBy 指定结果排序方式,跟$sort一样
output 指定分组元素输出的内容,可以是任何合法的表达式

用法

  • $top不支持作为聚合表达式。
  • $top只支持作为window 操作符
  • 聚合管道调用$top受100M的限制,如果单组超过这一限制将报错。

关于null和缺失值的处理

  • $top不会过滤掉空值,也就是空值也参与排序
  • $top会将缺失值转换为null,也就是缺失值会当做null排序

下面的聚合返回分组中得分最高的文档:

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",
         playerId:
            {
               $top:
                  {
                     output: [ "$playerId", "$score" ],
                     sortBy: { "score": -1 }
                  }
            }
      }
   }
] )

在这个例子中:

  • 使用$documents阶段创建了一些字面量(常量)文档,包含了选手的得分
  • $group阶段根据gameId对文档进行了分组,显然文档中的gameId都是G1
  • PlayerD的得分缺失,PlayerE的得分为null,他们的得分都会被当做null处理
  • playerId字段和score字段被指定为输出:["$playerId"," $score"],以数组的形式返回
  • 指定了排序的方式,按照score逆序:sortBy: { "score": -1 }
  • PlayerDPlayerE并列为第一的元素,PlayerD作为第一个score返回
  • 要解决因为多个元素空值导致的问题,就需要添加更多的字段参与到排序
js 复制代码
[
   {
      _id: 'G1',
      playerId: [ [ 'PlayerD', null ] ]
   }
]

举例

使用下面的命令,创建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 }
])

查找单个游戏的第一名

使用$top查找单场比赛的第一名:

js 复制代码
db.gamescores.aggregate( [
   {
      $match : { gameId : "G1" }
   },
   {
      $group:
         {
            _id: "$gameId",
            playerId:
               {
                  $top:
                  {
                     output: [ "$playerId", "$score" ],
                     sortBy: { "score": -1 }
                  }
               }
         }
   }
] )

在本例的管道中:

  • 使用$match阶段用一个gameId对结果进行筛选,即:G1
  • 使用$group阶段依据gameId对结果进行分组,本例中只有一个分组G1
  • 使用output : ["$playerId"," $score"]top指定输出字段
  • 使用sortBy: { "score": -1 }按照得分进行逆序排序
  • 使用$top返回游戏得分最高的元素

操作返回下面的结果:

js 复制代码
[ { _id: 'G1', playerId: [ 'PlayerC', 99 ] } ]

查所有游戏的第一名

使用$top查找所有游戏的第一名:

js 复制代码
db.gamescores.aggregate( [
      {
         $group:
         { _id: "$gameId", playerId:
            {
               $top:
                  {
                     output: [ "$playerId", "$score" ],
                     sortBy: { "score": -1 }
                  }
            }
         }
      }
] )

在本例的管道中:

  • 使用$group按照groupId对结果排序
  • 使用$top返回所有游戏中得分最低的
  • 使用output : ["$playerId", "$score"]指定top输出的字段
  • 使用sortBy: { "score": -1 }按照得分进行逆序排序

操作返回下面的结果:

js 复制代码
[
   { _id: 'G2', playerId: [ 'PlayerD', 80 ] },
   { _id: 'G1', playerId: [ 'PlayerC', 99 ] }
]
相关推荐
三84420 分钟前
sqli-labs1-10通关笔记
数据库
AndrewHZ25 分钟前
【LLM技术全景】阶段总结:技术原理篇核心知识回顾
人工智能·深度学习·算法·语言模型·大模型·llm·芯片开发
小星星闪亮登场1 小时前
2026萌新联赛第三场-- (郑州轻工业大学)
数据结构·c++·经验分享·算法·贪心算法·排序算法·深度优先
冻柠檬飞冰走茶2 小时前
PTA基础编程题目集 7-8超速判断(C++语言实现)
开发语言·数据结构·c++·算法
BullSmall2 小时前
Another Redis Desktop Manager(ARDM)下载指南
数据库·redis·缓存
SelectDB2 小时前
Apache Doris 倒排索引工作原理:全文检索提速 59 倍,点查提速 14 倍
数据库
码农颜2 小时前
5.1.1 原⼦性
数据库·mysql
机建狂魔2 小时前
Codex 接入第三方模型 API 实战:以 Mimo 为例
java·服务器·数据库·ai·ai编程·codex
玖玥拾2 小时前
LeetCode 88 合并两个有序数组
算法·leetcode
数据皮皮侠AI2 小时前
上市公司数字供应链金融指数(2010-2024)
大数据·人工智能·算法