Every single time I need to use $group
I find myself perplexed by how limited and wasteful this stage is in an otherwise really powerful pipeline system.
Why is it that $group
MUST destroy whatever was garnered in previous stages? Why was it not possible to design it to be akin to how $unwind
works, i.e. why can’t $group
store its results onto a new field without ruining whatever was aggregated in previous stages? This way a user would have a way to preserve their fields, and most importantly - easily perform chained $group
stages, and if they desired to obtain the shape that $group
does today, all they would need to do is throw in a $project
stage at the end.
What should one do to aggregate data with fields that have multiple $group'ings
on them? Suppose I have delivery status data:
[
{courier: "John Brown", productType: "Package", status: "DELIVERED"},
{courier: "John Brown", productType: "Insured Parcel", status: "DELIVERED"},
{courier: "John Brown", productType: "Package", status: "DELIVERY_RESCHEDULED"},
{courier: "Eve White", productType: "Bubble Mailer", status: "DELIVERY_FAILED"}
]
How do I run $group
in order to obtain the following:
Group by courier and and then group by delivery status:
For instance:
{
data: [
{
courier: "John Brown",
products: [
{
productType: "Package",
status: "DELIVERED",
count: 45
},
{
productType: "Package",
status: "DELIVERY_RESCHEDULED",
count: 2,
},
{
productType: "Insured Parcel",
status: "DELIVERED",
count: 21,
}
]
},
{
courier: "Eve White",
products: [
<...this courier's listings follow the same data structure...>
]}
]