当前位置: 首页 > news >正文

广州白云区最新信息网站布局优化怎么做

广州白云区最新信息,网站布局优化怎么做,企业网站整站,大连网站建设哪家公司好MongoDB聚合运算符#xff1a;$topN 文章目录 MongoDB聚合运算符#xff1a;$topN语法用法关于null和缺失值的处理BSON数据类型排序 举例查找三个得分最高的查找全部游戏中三个最高的得分基于分组key来计算参数n $topN聚合运算符返回分组中指定顺序的最前面 n个元素#xf…MongoDB聚合运算符$topN 文章目录 MongoDB聚合运算符$topN语法用法关于null和缺失值的处理BSON数据类型排序 举例查找三个得分最高的查找全部游戏中三个最高的得分基于分组key来计算参数n $topN聚合运算符返回分组中指定顺序的最前面 n个元素如果分组中的元素数量小于 n则返回分组的全部元素。从MongoDB5.2开始支持。 语法 {$topN:{n: expression,sortBy: { field1: sort order, field2: sort order ... },output: expression} }n用于限制每组结果的数量必须是正整数表达式要么是常数要么取决于$group的_id 值sortBy制定返回结果的顺序语法类似于$sortoutput指定分组元素输出的内容可以是任何合法的表达式。 用法 $topN不支持作为聚合表达式。$topN只支持作为window 操作符。聚合管道调用$topN受100M的限制如果单组超过这一限制将报错。 关于null和缺失值的处理 $topN不会过滤掉空值$topN会将缺失值转换为null 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:{$topN:{output: [ $playerId, $score ],sortBy: { score: 1 },n: 3}}}} ] )在这个例子中 使用$documents阶段创建了一些字面量常量文档包含了选手的得分$group阶段根据gameId对文档进行了分组显然文档中的gameId都是G1PlayerD的得分缺失PlayerE的得分为null他们的得分都会被当做null处理playerId字段和score字段被指定为输出[$playerId, $score]以数组的形式返回sortBy: { score: 1 }指定了排序的方式空值被排在最前面返回playerId数组 如下 [{_id: G1,playerId: [ [ PlayerD, null ], [ PlayerE, null ], [ PlayerA, 1 ] ]} ]BSON数据类型排序 当不同类型排序是使用BSON数据类型的顺序进行排序 当进行正序排序时由小到大字符串的优先级在数值之前当进行逆序排序时由大到小字符串的优先级在数值之前 下面的例子中包含了字符串和数值类型 db.aggregate( [{$documents: [{ playerId: PlayerA, gameId: G1, score: 1 },{ playerId: PlayerB, gameId: G1, score: 2 },{ playerId: PlayerC, gameId: G1, score: }]},{$group:{_id: $gameId,playerId: {$topN:{output: [$playerId,$score],sortBy: {score: -1},n: 3}}}} ] )在这个例子中 PlayerA的得分是整数1PlayerB的得分是字符串2PlayerC的得分是空字符串 因为排序指定为逆序{ score : -1 }字符串的字面量排在PlayerA的数值得分之前 [{_id: G1,playerId: [ [ PlayerB, 2 ], [ PlayerC, ], [ PlayerA, 1 ] ]} ]举例 使用下面的命令创建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 } ])查找三个得分最高的 使用$topN查找单个游戏中得分最高的3个 db.gamescores.aggregate( [{$match : { gameId : G1 }},{$group:{_id: $gameId,playerId:{$topN:{output: [$playerId, $score],sortBy: { score: -1 },n:3}}}} ] )本例中 使用$match阶段用一个gameId对结果进行筛选即G1使用$group阶段依据gameId对结果进行分组本例中只有一个分组G1使用sortBy: { score: -1 }按照得分进行逆序排序使用output : [$playerId, $score]为$topN指定输出字段使用$topN返回游戏得分最高的3个选手和得分 结果如下 [{_id: G1,playerId: [ [ PlayerC, 99 ], [ PlayerB, 33 ], [ PlayerA, 31 ] ]} ]与下面的SQL查询等价 SELECT T3.GAMEID,T3.PLAYERID,T3.SCORE FROM GAMESCORES AS GS JOIN (SELECT TOP 3GAMEID,PLAYERID,SCOREFROM GAMESCORESWHERE GAMEID G1ORDER BY SCORE DESC) AS T3ON GS.GAMEID T3.GAMEID GROUP BY T3.GAMEID,T3.PLAYERID,T3.SCOREORDER BY T3.SCORE DESC查找全部游戏中三个最高的得分 使用$topN查找所有游戏中得分最高的三个 db.gamescores.aggregate( [{$group:{ _id: $gameId, playerId:{$topN:{output: [ $playerId,$score ],sortBy: { score: -1 },n: 3}}}} ] )在本例中 使用$group按照groupId对结果排序使用output : [$playerId, $score]指定bottom输出的字段使用sortBy: { score: -1 }按照得分进行逆序排序使用$topN返回所有游戏中得分最高的三个 结果如下 [{_id: G1,playerId: [ [ PlayerC, 99 ], [ PlayerB, 33 ], [ PlayerA, 31 ] ]},{_id: G2,playerId: [ [ PlayerD, 80 ], [ PlayerC, 66 ], [ PlayerB, 14 ] ]} ]这个操作与下面的SQL语句等价 SELECT PLAYERID,GAMEID,SCORE FROM(SELECT ROW_NUMBER() OVER (PARTITION BY GAMEID ORDER BY SCORE DESC) AS GAMERANK,GAMEID,PLAYERID,SCOREFROM GAMESCORES ) AS T WHERE GAMERANK 3 ORDER BY GAMEID基于分组key来计算参数n 可以动态指定n的值在本例中$cond表达式用在gameId字段 db.gamescores.aggregate([{$group:{_id: {gameId: $gameId},gamescores:{$topN:{output: $score,n: { $cond: { if: {$eq: [$gameId,G2] }, then: 1, else: 3 } },sortBy: { score: -1 }}}}} ] )在本例中 使用$group按照groupId对结果排序使用output : $score指定$topN输出的字段如果gameId是G2则n为1否则n为3使用sortBy: { score: -1 }按照得分进行逆序排序 操作结果如下 [{ _id: { gameId: G1 }, gamescores: [ 99, 33, 31 ] },{ _id: { gameId: G2 }, gamescores: [ 80 ] } ]
http://www.w-s-a.com/news/775945/

相关文章:

  • 深圳企业网站建设推广服务php做的商城网站设计论文
  • 韩雪冬网站手机网站开发 宽度
  • 奉贤专业做网站新手怎么做企业网站
  • 做网站用哪几个端口 比较好手机号网站源码
  • 手机免费代理ip网站那个旅游网站做攻略最好
  • 西安做网站找哪家公司好苏州专业网站建设开发
  • dedecms如何做网站网站设计实施方案
  • 网站建设合约品牌设计有哪些
  • 织梦企业门户网站宝塔搭建wordpress网站
  • 网站为什么没有排名了11月将现新冠感染高峰
  • 网站开发维护专员岗位职责辽阳企业网站建设
  • 做外国订单有什么网站网站设计论文提纲
  • 商城网站建设报价方案导师让做网站
  • 清远市企业网站seo联系方式动易官方网站
  • 手机上怎么做能打开的网站一级域名和二级域名跨域
  • 网站首页效果图wordpress 在线教育
  • 电商网站开发团队广西桂林商贸旅游技工学校
  • 网站模板文件怎么下载东莞常平镇邮政编码
  • 建网站需要什么wordpress误删的后果
  • wordpress无插件实现网站地图做阿里巴巴网站店铺装修费用
  • 英文互动网站建设南宁住房和城乡建设局网站
  • 威海微网站建设乐清建网站哪家强
  • 网站和app的开发成本saas系统开发教程
  • ps切片工具做网站大气简洁网站
  • 网至普的营销型网站建设wordpress邮箱验证插件下载
  • 找权重高的网站方法张家港早晨网站建设
  • WordPress数据库添加管理员关键词优化举例
  • 河南国基建设集团--官方网站wordpress qode
  • 做农村电子商务的网站有哪些内容静态网站模板古典
  • 导航网站设计方案个人网站推广方法