The field "$name" must be an accumulator object
There are some aggregation operators that can only be used in $group
aggregation and named as $group accumulators
Just as you used $sum
here you have to use for the name
key as well
{ "$group": {
"_id": "$_id",
"name": { "$first": "$name" }, //$first accumulator
"count": { "$sum": 1 }, //$sum accumulator
"totalValue": { "$sum": "$value" } //$sum accumulator
}}
Accumulator is like array of Elements its Accumulates as Array. $first -> gives 1st name that goes in the group of names
Example:
so if you have $_id
same but different name ["Darik","John"]
specifying $first
will give Darik & similarly $last
will give John