db.collections.find ()
{ "_id" : ObjectId("55b0c2a0339bf8d00ab0bade"), "score" : 46, "playerid" : "45"}
{ "_id" : ObjectId("55b0c2de339bf8d00ab0badf"), "score" : 88, "playerid" : "45"}
{ "_id" : ObjectId("55b0cbca17f398f4281ab931"), "score" : 46, "playerid" : "99"}
{ "_id" : ObjectId("55b15ababe2df0f430d1cb93"), "score" : 89, "playerid" : "45"}
What I'm trying to do is get all the documents with the highest score. If the same player meets more than once, we get the document with the highest score for this particular player.
The result will look like this:
{ "_id" : "55b0cbca17f398f4281ab931", "score" : 46 }
{ "_id" : "55b15ababe2df0f430d1cb93", "score" : 89 }
This is where I am stuck:
db.players.aggregate([
{ "$group": {
"_id": "$playerid",
score: { $max: "$score" }
} }
])
which returns:
{ "_id" : "99", "score" : "46" }
{ "_id" : "45", "score" : "89" }
Now this is correct. But I just need an ObjectID.
source
share