After Mongo group is grouped, how to sort the grouped results from big to small and take the first 5

  mongodb, question
BasicDBObject key = new BasicDBObject("ip",true);
 BasicDBObject initial = new BasicDBObject("count",0);
 BasicDBObject cond = 。  .  .  .
 String reduce = "function(obj,prev){prev.count+=1}";
 String finalize =?  ?  ?  ?  ?
 
 col.group(key,initial,cond,reduce,finalize);

The final result, I want tocountThe quantity is arranged from large to small, and only the first five can be used.finalizeDid it happen? How to write the function? ? Or by what means? ? ? ?

To be honest, the group-by under mongo is still troublesome. Native mongo statement, query aggregated through pipeline

sqldb.term.aggregate([
 {$match:{library_id:3607}},
 {$limit:5},
 {$group:{_id:"$version", count: {$sum:1}}},
 {$sort:{count:-1}}
 ])

term: The name of the collection you want to query
$match: match criteria, optional
$limit: Number of results, optional
$group: aggregation rule
$sort: Sort query results, -1 indicates descending order

http://docs.mongoing.com/manual-zh/reference/operator/aggregation.html#aggregation-pipeline-operator-reference