Gunakan perintah ini, ini akan berfungsi untuk kasus sampel Anda, semoga ini akan memperbaiki masalah Anda,
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"
}
}
}
}])
keluaran di shell :
{
"_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
}]
}