文章目录
- 语法
- 使用
- 空和缺失值的处理
- m a x N 和 maxN和 maxN和topN
- 关于窗口功能和聚合表达式的支持
- 关于内存的限制
- 举例
- 找出一个项目中前三名的得分
- 查找多个比赛项目中得分最高的三个
- 基于$group分组的Key计算n
$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
对文档进行分组,例子中只有一个gameId
,G1
PlayerD
的得分缺失,PlayerE
的得分为空,这些值都被视为空$maxN
和input:"$score"
返回一个数组放在maximumThreeScores
字段- 尽管
n=4
,但因为只有3个文档有得分,$maxN
只返回得分最高的3个
[{_id: 'G1',maximumThreeScores: [ 3, 2, 1 ]}
]
m a x N 和 maxN和 maxN和topN
$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
筛选出gameId
为G1
的项目 - 使用
$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
指定输入字段 - 如果
gameId
为G2
则n
为1,否则n
为3
操作返回结果:
[{ _id: { gameId: 'G2' }, gamescores: [ [ 80, 'PlayerD' ] ] },{_id: { gameId: 'G1' },gamescores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ]}
]