I'm trying to reach something like this: I have collections of activities that belong to some user. I want to get the activity names distincted ordered by 'added_time', so I used 'group by' on the activity name and get the max value of 'added_time'. Also, I want to sort them by 'added_time', and then to get the whole document. The only thing that I reached so far, is to get only the name that I grouped by, and the 'added_time' property. This is the query:
db.getCollection('user_activities').aggregate
(
{$match: {'type': 'food', 'user_id': '123'}},
{$group:{'_id':'$name', 'added_time':{$max:'$added_time'}}},
{$sort:{'added_time':-1}},
{$project: {_id: 0,name: "$_id",count: 1,sum: 1, 'added_time': 1}}
)
Can someone help me with reaching the whole document? Thank's!