I have a mongo aggregation query that is currently grouping results based on a location id, and using $push to return a count value using $size of elements in an array for the resulting documents matching a given date. I need to adjust the output for each result grouped by location id and $sum the count values grouped by date. Examples below.
The current result output resembles:
[{
"_id" : "100",
"appts" : [
{
"count" : NumberInt(2),
"date" : "2020-08-07"
},
{
"count" : NumberInt(2),
"date" : "2020-08-07"
} ]
},
{
"_id" : "103",
"appts" : [
{
"count" : NumberInt(1),
"date" : "2020-08-07"
},
{
"count" : NumberInt(3),
"date" : "2020-08-07"
},
{
"count" : NumberInt(2),
"date" : "2020-08-08"
} ]
}]
I'm attempting to produce the following output:
[{
"_id" : "100",
"appts" : [
{
"count" : NumberInt(4),
"date" : "2020-08-07"
} ]
},
{
"_id" : "103",
"appts" : [
{
"count" : NumberInt(4),
"date" : "2020-08-07"
},
{
"count" : NumberInt(2),
"date" : "2020-08-08"
} ]
}]
My current query:
[
{ $match: { ... } },
{
$group: {
_id: {
date: { $dateToString: { format: '%Y-%m-%d', date: '$time' } },
loc: '$location.branchId',
additionalReminders: '$analytics.twilio.additionalReminders'
}
}
},
{
$group: {
_id: '$_id.loc',
appts: {
$push: {
count: { $size: '$_id.additionalReminders' },
date: '$_id.date'
}
}
}
}
]
Solution: Adding the following query provided by @Rfroes87 to the end of the pipeline solved the issue.
{ $unwind: "$appts" },
{
$group: {
_id: { "id": "$_id", "date": "$appts.date" },
"count": { $sum: "$appts.count" }
}
},
{
$group: {
_id: "$_id.id",
"appts": { $push: { "count": "$count", "date": "$_id.date" } }
}
}
Probably you can try, not sure with your document structure,
Predicted document structure:
Playground