0

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}
]); 

2 Answers 2

2

you can use the $max operator in aggregation:

db.playerscorecollection.aggregate([
 { $unwind: "$scorearray"},
 { $group: { _id: "$player", maxScore: { $max: "$scorearray.score" } } }
])

----- For your second question to find for each player the max score and the venue at which that max score happened:

db.playerscorecollection.aggregate(
      { $unwind: "$scorearray"},
      { $group: { _id: { player: "$player", venue: "$scorearray.venue", score: "$scorearray.score" } } },
      { $sort: { "_id.score" : 1 } },
      { $group: { _id: "$_id.player", maxScore: { $last: "$_id.score" }, venue: { $last: "$_id.venue"} } }
)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for an excellent answer , I just realized that I need to add venuealso to the score array , what if I need to get the max score for a player and the venue the game was played at also ?
Thank you for an excellent answer , I just realized that I need to add venue also to the score array , I need to get the max score for a player and the venue the game was played at also ? I tried adding venue to the group ,but I get an error that says "the group aggregate field 'venue' must be defined as an expression inside an object". Can you please help ?
Thank you for my second question I achieved it using the following simpler way instead.Now I need guidance in tranlating it to a Java program that uses a Java DB driver to retrieve data. Any guidance would be really appreciated. db.playerscorecollection.aggregate([ { $unwind: "$scorearray"}, { $group: { _id: {player:'$player,venue:'$venue'}, maxScore: { $max: "$scorearray.score" } } } ]);
0

Thank you for my second question, I achieved it using the following simpler way instead.

Now I need guidance in translating it to a Java program that uses a Java DB driver to retrieve data. Any guidance would be really appreciated.

db.playerscorecollection.aggregate([ 
{ $unwind: "$scorearray"}, 
{ $group: { _id: {player:'$player,venue:'$venue'}, 
maxScore: { $max: "$scorearray.score" } } } ]); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.