My mongoDB collection is structured like this:
[
{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}
]
I am aiming to identify the most recent event for each user, resulting in a set of data as follows:
[
{user: "rick", event: "bork", timestamp: 3},
{user: "joe", event: "bing", timestamp: 4},
{user: "meg", event: "biz", timestamp: 5},
{user: "tim", event: "boz", timestamp: 6}
]
Although I have reviewed the mongoose aggregation framework documentation, I haven't found a direct solution. Many examples group by user but lose some details in the process. I am unsure how to preserve the complete documents at the end.