在mongodb中如何在group后取每个分组的前三条数据

在mongodb中如何在group后取每个分组的前三条数据,我现在的语句是
db.getCollection('Recommend').aggregate([{ $match : { imgCount :{$gt : 0},maxWeight :{$gt : 0.1}}},
{$sort : {msgType : -1,time : -1 }},
{$limit : 10000},
{$group : {_id : "$topic", rowkey : {$first : "$rowkey"}}}
])取得每个分组第一条数据,要如何写可以取前三条?

  • -自己解决了后面再加一个project的管道; db.getCollection('Recommend').aggregate([{ $match : { imgCount :{$gt : 0},maxWeight :{$gt : 0.1}}}, {$sort : {msgType : -1,time : -1 }}, {$limit : 10000}, {$group : {_id : "$topic", rowkey : {$push : "$rowkey"}}}, {$project :{_id : 1,rowkey :{$slice :["$rowkey",0 ,2]}}} ])

只能group得到数据后,自己客户端再去解析获取前三条