Använd det här kommandot, det kommer att fungera för ditt exempelfall, hoppas det löser ditt problem ,
db.testCollection.aggregate([{
$group: {
_id: {
ASSIGN_ID: "$ASSIGN_ID",
STATUS: "$STATUS"
},
count: {
"$sum": 1
}
}
}, {
$group: {
_id: "$_id.ASSIGN_ID",
STATUS_GROUP: {
$push: {
STATUS: "$_id.STATUS",
count: "$count"
}
}
}
}])
utdata i skal:
{
"_id": "583f84bce58725f76b322398",
"STATUS_GROUP": [{
"STATUS": 3,
"count": 1
}, {
"STATUS": 4,
"count": 2
}, {
"STATUS": 1,
"count": 1
}]
}, {
"_id": "583f84bce58725f76b322311",
"STATUS_GROUP": [{
"STATUS": 3,
"count": 1
}, {
"STATUS": 1,
"count": 1
}]
}, {
"_id": "583f84bce58725f76b322322",
"STATUS_GROUP": [{
"STATUS": 1,
"count": 1
}, {
"STATUS": 4,
"count": 1
}]
}