I have the following documents in mongoDB:
{
_id: "YYYY-MM-DD",
workedTimes: [
{..., driver: {username: string} }
]
}
every workedTime has a driver with a userName prop, and i want to group them by date and driver.username with a aggregation but i can't get it
I've tried something like this
$group: {
"_id": "$workedTimes.driver.username",
"date": {
$push: "$_id"
},
"clocks": {
$push: {
"date": "$_id",
"workedTimes": "$workedTimes"
}
}
}
but i cant get the output i want
I want a output something like this
{
date: 2022-08-24
username: driver1
workedTimes: []
},
{
date: 2022-08-24
username: driver2
workedTimes: []
},
{
date: 2022-08-25
username: driver1
workedTimes: []
}