I am trying to convert this sentence using the Mongoose Aggregate method :
\"For each player with given oid, select the game that has been played the most\". Here is my
You can use $first to take values from the first doc in each group of a sorted pipeline:
var allids = ['xxxxx','yyyy'];
Game.aggregate([
{$match: {'oid': {$in:allids}}},
{$sort: {'number_plays': -1}},
{$group: {
_id: '$oid',
game_name: {$first: "$game_name"},
game_id: {$first: "$game_id"},
number_plays: {$first:"$number_plays"}
}}
], function(err,list){
console.log(list);
res.end();
});
Because you've sorted on number_plays
descending in the preceding stage of the pipeline, this will take the values from each oid
group's doc with the highest number_plays
.