I have a collection of user data entries that I need to group by usernames/ids and sort by number of times they appear for each user (a sort of top list of entries).
I managed to achieve it with this code:
Entries.aggregate([
{
$group: {
_id: { uid: "$uid", name: "$name" },
count: { $sum: 1 },
utime: { $max: "$utime" }
}
},
{ $match: { count: { "$gt": 0 } } }
]);
Counting and matching all entries greater than 0 and giving precedence to older entries (with utime), if count is same on different users.
What I need now is to include a custom date range between which same grouping would occur, since this was applied to entire table. Native mongodb timestamps are in effect.
How can I achieve this? Any suggestions would be greatly appreciated.