I have data in mongo collection in the following format:
{
"response":[
{
"_responsedata":{
"Error":{
"message":"BMERROR001 - Something went wrong. Please check the error logs",
"statusCode":"400",
"reasonCode":"BMERROR001"
}
},
"_responsemeta":{
"status":"400"
}
},
{
"_responsedata":{
"Error":{
"message":"BMERROR001 - Something went wrong. Please check the error logs",
"statusCode":"400",
"reasonCode":"BMERROR001"
}
},
"_responsemeta":{
"status":"400"
}
},
{
"_responsedata":{
"Error":{
"message":"BMERROR002 - Something went wrong. Please check the error logs",
"statusCode":"400",
"reasonCode":"BMERROR002"
}
},
"_responsemeta":{
"status":"400"
}
},
{
"_responsedata":{
"name":"name1",
"col1":"value1"
},
"_responsemeta":{
"status":"204"
}
},
{
"_responsedata":{
"name":"name2",
"col1:":"value2"
},
"_responsemeta":{
"status":"201"
}
},
{
"_responsedata":{
"Error":{
"message":"BMERROR003 - Something went wrong. Please check the error logs",
"statusCode":"400",
"reasonCode":"BMERROR003"
}
},
"_responsemeta":{
"status":"400"
}
}
]
}
This ihe format of one documents and I have many documents in the same format. Now for making this documents in a format, which can be visualized using D3, I need to group them based on the reasonCode field. So I need something like this as a result :
{
"errors": [
{
"code":"BMERROR001",
"count":2
},
{
"code":"BMERROR002"
"count":"1"
},
{
"code":"BMERROR003"
"count":"1"
}
]
}
I tried with the following aggregation, but it is giving the reason as an array and the count.
[{$group: {
_id:{
"reason":"$response._responsedata.Error.reasonCode"
},
count:{$sum:1}
}}]
How can I take the count grouped by the reasonCode properly ?
$unwindbefore$group.{ $unwind: '$response' }as it is an array.