Hey all,
I’m working on grouping and totalling log events (group by user and total of events of each type for each user). I’ve made some progress but I could use some pointers to help me get the results I’m looking for.
My code so far
[
// Find all tester events in current month
{
$match: {
'user.role': 'tester',
datetime: {
$gte: moment().startOf('month').toDate(),
$lte: moment().endOf('month').toDate(),
},
},
},
// Group by tester + event type and project the total
{
$group: {
_id: { testerId: '$user._id', eventId: '$eventId' },
total: { $sum: 1 },
},
},
]
This is giving me the user Id and event type with total for each event type, but all at the “top level” and not as I intend, which would be something like this:
[
{
_id: [user _id],
user: [whole $user object],
events: {
'login': 10,
'test-completed': 3,
'password-reset': 1
}
}
]
I hope that this makes sense, if I’m not making sense please let me know and I’ll try to clarify.
Thanks for any help you can offer