I have a data collection containing data in the following shape:
[ {_id: "1",
aa1: 45, aa2: 56,
bb1: 90, bb2: 78,
cc1: 34, cc2: 98 },
{_id: "2",
aa1: 76, aa2: 56,
bb1: 45, bb2: 67,
cc1: 75, cc2: 87 } ]
on this data I perform a MongoDB aggregation pipeline:
db.colleciton.aggregate([
{ $project: {
"_id": "$_id",
"aaa": { $avg: ["$aa1","$aa2"] },
"bbb": { $avg: ["$bb1","$bb2"] },
"ccc": { $avg: ["$cc1","$cc2"] }
}
},
{ $group: {
"_id": "AvgCalc",
"aaa": { $avg: "$aaa" },
"bbb": { $avg: "$bbb" },
"ccc": { $avg: "$ccc" }
}
}
])
that gives my result in the format:
[ {_id: "AvgCalc",
aaa: 67,
bbb: 55,
ccc: 90} ]
I would like to convert this in the aggregation to give my result as:
[ {field: "aaa", value: 67},
{field: "bbb", value: 55},
{field: "ccc", value: 90} ]
Is there a way to achieve this with aggregation? I would like to add this as a final stage in the pipeline. I tried adding a $project stage but with no success.