Anda harus mencoba $facet
agregasi untuk mendapatkan hasil yang diinginkan yang cukup mudah digunakan dengan limit
dan skip
...
Anda dapat memeriksa output di sini
db.collection.aggregate([
{ "$facet": {
"top": [
{ "$group": {
"_id": "$Category",
"response": { "$sum": "$response" }
}},
{ "$sort": { "response": -1 }},
{ "$limit": 2 }
],
"rest": [
{ "$group": {
"_id": "$Category",
"response": { "$sum": "$response" }
}},
{ "$sort": { "response": -1 }},
{ "$skip": 2 },
{ "$group": {
"_id": "Others",
"response": { "$sum": "$response" }
}}
]
}},
{ "$project": { "data": { "$concatArrays": ["$top", "$rest"] }}},
{ "$unwind": "$data" },
{ "$replaceRoot": { "newRoot": "$data" }}
])