我在我的收藏中只是简单地分配了人员和状态,如下所示
[
{"ASSIGN_ID": "583f84bce58725f76b322398", "STATUS": 1},
{"ASSIGN_ID": "583f84bce58725f76b322398","STATUS": 4},
{"ASSIGN_ID": "583f84bce58725f76b322398","STATUS": 4},
{"ASSIGN_ID": "583f84bce58725f76b322398","STATUS": 3},
{"ASSIGN_ID": "583f84bce58725f76b322311","STATUS": 1},
{"ASSIGN_ID": "583f84bce58725f76b322311","STATUS": 3},
{"ASSIGN_ID": "583f84bce58725f76b322322","STATUS": 1},
{"ASSIGN_ID": "583f84bce58725f76b322322","STATUS": 4}
]
我希望您能按照以下方式将此数据分组:ASSIGN_ID,并在其中按STATUS进行分组,计算每个STATUS的数量,如下所示。
[
{
"ASSIGN_ID":"583f84bce58725f76b322398",
"STATUS_GROUP":[
{
"STATUS":1,
"COUNT":1
},
{
"STATUS":3,
"COUNT":1
},
{
"STATUS":4,
"COUNT":2
}
]
},
{
"ASSIGN_ID":"583f84bce58725f76b322311",
"STATUS_GROUP":[
{
"STATUS":1,
"COUNT":1
},
{
"STATUS":3,
"COUNT":1
}
]
},
{
"ASSIGN_ID":"583f84bce58725f76b322322",
"STATUS_GROUP":[
{
"STATUS":1,
"COUNT":1
},
{
"STATUS":4,
"COUNT":1
}
]
}
]
然而,我已编写代码,仅按 状态 分组。请查看以下查询。
Inspection.aggregate([
{$group: {
"_id": '$STATUS',
"count" : { $sum : 1 }}}], function (err, result) {
}});
请帮我修复这个问题。