Filter record from collection “OperationSession”, sort by “WorldId” descending, then group by “WorldId”, then pick first record from each group, then sort result: Way #1: db.getCollection('OperationSession').aggregate( [ { "$match": {"ActivityId":74,"GameId":2109} }, { "$sort":{ "CreateTime" : -1} }, { "$group": { _id:"$WorldId", SessionId:{"$first": "$_id" }, GameId:{"$first": "$GameId" }, WorldId:{"$first": "$WorldId" }, ActivityId:{"$first": "$ActivityId" }, Type:{"$first": "$Type" …
Tag: Aggregate