I have a projects collection:
{
_id: 1,
title: "Project 1"
},
{
_id: 2,
title: "Project 2"
}
and a (time) entries collection:
{
_id: 90123,
project_id: 1,
task_id: 1,
hours: 3
},
{
_id: 90124,
project_id: 1,
task_id: 1,
hours: 5
},
{
_id: 90125,
project_id: 2,
task_id: 2,
hours: 1
},
{
_id: 90126,
project_id: 1,
task_id: 2,
hours: 2
}
I'd like to use a pipeline aggregation to:
- Get entries for project 1
- Sum all entries for project 1 as "totalSpent"
- Group by task_id with summed hours per task
Something like this as the end result:
{
totalSpent: 10,
spentByTask: {
{ task_id: 1, spent: 8 },
{ task_id: 2, spent: 2 }
}
}