I’ve got a mongoDB collection that looks like:
[
{user: "joe", event: "foo", timestamp: 1},
{user: "meg", event: "blah", timestamp: 2},
{user: "rick", event: "bork", timestamp: 3},
{user: "joe", event: "bing", timestamp: 4},
{user: "meg", event: "biz", timestamp: 5},
{user: "tim", event: "boz", timestamp: 6}
]
and I want to find the latest event for each user, so get a set of results like:
[
{user: "rick", event: "bork", timestamp: 3},
{user: "joe", event: "bing", timestamp: 4},
{user: "meg", event: "biz", timestamp: 5},
{user: "tim", event: "boz", timestamp: 6}
]
I’ve looked over the mongoose aggregation framework docs, and it seems like there should be a way to do this simply, but none of the examples I’ve seen do quite exactly this. Lots of them group by user, but then the resulting groupings lose some of the data, and I’m not sure how to reconstitute the full documents at the end.
2
Answers
$setWindowFields
– Partition/Group the document by theuser
field, order each partition bytimestamp
descending, and add therank
field with the$rank
operator.$match
– Filter the document withrank: 1
indicates getting the document with the latest timestamp for each partition.$sort
– Order the document bytimestamp
ascending.$unset
– Remove therank
field from the documents.Demo @ Mongo Playground
you can use a combination of sort group and project, i have solved for you
in this way you can solve without losing any document info