In one aggregation stage within my MongoDB view pipeline I am using $group
. One of the things I'm doing within that $group
stage is summing up an openBalance
, like this:
"openBalance": {
"$sum": "$openBalance"
},
This works.
However, I also need to get another value, that first matches to a condition, and then sums data. In other words, something like this:
"customerResponsibleOpenBalance": {
"$match": {
"customerResponsible": true
},
"$sum": "$openBalance"
},
However, I can't do this like this within a $group
stage, because it results in this error:
customerResponsibleOpenBalance
must specify one accumulator.
So how would I go about doing this in a different way? Are $facets
my only option here?