I need help sorting an unread message collection based on user and timestamp. My goal is to group messages by users while keeping the most recent ones at the top. I attempted the following approach:
UnreadMessages.find({},
{sort: {'message.timestamp': -1, 'fromUser._id': 1} });
However, this method does not properly group messages by user.
Is there a more effective way to display the latest messages grouped by user? Any suggestions would be greatly appreciated.