I have a collection of objects that need to be grouped by masterID or id if masterID is absent. Of a group of objects with the same masterID I need to find the one with the highest pos attribute ($max: "$pos"). However, I struggle to get the complete object where pos is maximized, it seems via aggregation I can only get the pos attribute, but not the whole object. Here is the sample aggregation I use, lacking the $ifNull for masterID:
> db.tst.aggregate([{ "$group": { "_id": "$masterID", "pos": { "$max": "$pos" } } }])
Sample objects would be:
> db.tst.find()
{ "_id" : ObjectId("547d6bd28e47d05a9a492e2e"), "masterID" : "master", "pos" : "453", "id" : "hallo" }
{ "_id" : ObjectId("547d6bda8e47d05a9a492e2f"), "masterID" : "master", "pos" : "147", "id" : "welt" }
{ "_id" : ObjectId("547d6be68e47d05a9a492e30"), "masterID" : "master2", "pos" : "1", "id" : "welt" }
The wanted aggregation result is:
{ "_id" : ObjectId("547d6bd28e47d05a9a492e2e"), "masterID" : "master", "pos" : "453", "id" : "hallo" }
{ "_id" : ObjectId("547d6be68e47d05a9a492e30"), "masterID" : "master2", "pos" : "1", "id" : "welt" }
Is there a way to achieve this result via aggregation, or do I need to use $push to obtain all grouped objects and implement the maximisation on pos in the Java code around the aggregation?
This question mongodb aggregation: How to return a the object with min/max instead of the value indicates that $first or $last should be used on the objects sorted by pos, but I fail to see how that returns the whole object.