You can use $redact to accomplish this. This avoids the use of $sort , and then runs $group or $unwind again.
$group on _id and get maximum max_num_sold for each group, copy all documents in the group using $push .$redact in subtopies per group, storing only those with a maximum max_num_sold in their num_sold
code example:
db.getCollection('sales').aggregate([ {$group:{"_id":"$hash", "max_num_sold":{$max:"$num_sold"}, "records":{$push:"$$ROOT"}}}, {$redact:{$cond:[{$eq:[{$ifNull:["$num_sold","$$ROOT.max_num_sold"]}, "$$ROOT.max_num_sold"]}, "$$DESCEND","$$PRUNE"]}}, ])
test data:
db.getCollection('sales').insert([ {"title":"Foo","hash":17,"num_sold":49,"place":"ABC"}, {"title":"Bar","hash":18,"num_sold":55,"place":"CDF"}, {"title":"Baz","hash":17,"num_sold":55,"place":"JKN"}, {"title":"Spam","hash":17,"num_sold":20,"place":"ZSD"}, {"title":"Eggs","hash":18,"num_sold":20,"place":"ZDF"} ])
test result:
{ "_id" : 18, "max_num_sold" : 55, "records" : [ { "_id" : ObjectId("567874f2b506fc2193a22696"), "title" : "Bar", "hash" : 18, "num_sold" : 55, "place" : "CDF" } ] } { "_id" : 17, "max_num_sold" : 55, "records" : [ { "_id" : ObjectId("567874f2b506fc2193a22697"), "title" : "Baz", "hash" : 17, "num_sold" : 55, "place" : "JKN" } ] }