I have the following documents in my mongoDB collection.
{
"_id" : ObjectId("51d90c3746503d6ab5933b88"),
"scorearray" : [
{
"score" : 0.0
},
{
"score" : 7.0
}
],
"player" : "Arod"
}
/* 1 */
{
"_id" : ObjectId("51d90c0d46503d6ab5933b86"),
"scorearray" : [
{
"score" : 2.0
},
{
"score" : 1.0
},
{
"score" : 5.0
}],
"player" : "Martini"
}
I am trying to get the max score for each player and I have a query as below , but it gives me just one player i.e Arod with 7.0 , I need both Arod 7.0 and Martini 5.0 in my query result .
Can some one please guide me on how to modify the below query.Any guidance would be greatly appreciated.
db.playerscorecollection.aggregate([
{$project:{_id:0,player:1, "scorearray.score":1}},
{$unwind:"$scorearray"},
{$sort:{"scorearray.score":-1}},
{$limit:1}
]);