Hi @steevej
tried to tweak your suggestion and I have 2 problems,
- is totally beginner: in your two $group stage in some cases you use “” around “_id”, and “$group”, in some cases you don’t. Why is the difference?
- I try to use $facet stage after a $group stage. I can not export the pipeline in Compass properly, the $facet stage has errors and that is not shown in the exported version
This $group stage is basically an extended version of your first $gropup stage:
$group: {
_id: {
"email": "$order.billing.email",
"globalOrderId": "$global.order_id",
"status": "$order.status"
},
"countEach": {
"$sum": 1
}
}
My source document example is:
{
"_id": {
"$oid": "5ffb36c87f9b06d538fa78a3"
},
"order": {
"billing": {
"email": "buyer_3@example.com",
"phone": "+420 296 631 111"
},
"id": {
"$numberInt": "1465"
},
"status": "pending",
"currency": "EUR",
"date_created": {
"$date": {
"$numberLong": "1610298684000"
}
},
"date_modified": {
"$date": {
"$numberLong": "1610299059000"
}
},
"total": {
"$numberDouble": "140.94"
},
"customer_id": {
"$numberInt": "7"
},
"customer_ip_address": null,
"customer_user_agent": null,
"customer_note": "VA customer note",
"payment_method": "other",
"date_paid": null,
"date_completed": null
},
"numVerify": {
"valid": true,
"international_format": "+420296631111",
"country_prefix": "+420",
"country_code": "CZ",
"location": "Praha"
},
"businessMeta": {
"client_id": "0001",
"client_name": "Acme Co",
"webshop_id": "00001",
"webshop_name": "exampleshop.com"
},
"global": {
"order_id": "00001-0000-1465",
"customer_id": "00001-00001-7"
}
}
since I added one more items to { _id } in first $group stage, I want to use $facet instead of 2nd $group, when the different sub-pipelines contains the different $groups ( similarly to you 2nd $group stage).
My issue is I got a syntax error and I can not figure out what is wrong.
The content in the 2nd stage, in the $ facet block is:
/**
* outputFieldN: The first output field.
* stageN: The first aggregation stage.
*/
{
"groupedByEmail":
[
{ $match: { "_id.email": { $exists: 1 } } },
{ $group: {
_id: "$_id.email",
"counts_ByEmail":
{
$push:
{
"globalOrderId": "$_id.globalOrderId",
"status": "$_id.status",
"count_ByEmail": "$countEach"
}
}
}
}
],
"groupedByGOrderId":
[
{ $match: { "$_id.globalOrderId": { $exists: 1 } } },
{ $group: {
_id: "$_id.globalOrderId",
"counts_ByGOrderId":
{
$push:
{
"email": "$_id.email",
"status": "$_id.status",
"count_ByGOrderId": "$countEach"
}
}
}
}
],
"groupedByStatus":
[
{ $match: { "$_id.email": { $exists: 1 } } },
{ $group: {
_id: "$_id.status",
"counts_ByStatus":
{
$push:
{
"globalOrderId": "$_id.globalOrderId",
"status": "$_id.status",
"count_ByStatus": "$countEach"
}
}
}
}
]
}
EDIT: The latest error message is “unknown top level operator: $_id.globalOrderId” -EDIT END
EDIT 2: When I commented out all 3 $match stages, it works properly, so there must be the problem! -EDIT 2 END
So what is wrong with it and you might noticed that I extended your solution from grouping by 2 items to by 3, so how would you do it instead of mine?
Thanks a lot!