1 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊
您可以運(yùn)行$group兩次以prepaid首先計(jì)數(shù),然后您可以應(yīng)用$arrayToObject來獲取Y/N對象鍵:
db.collection.aggregate([
{
$group: {
_id: { city: "$City", prepaid: "$Prepaid" },
total: { $sum: 1 }
}
},
{
$grop: {
_id: "$_id.city",
Count: {
$push: {
k: "$_id.prepaid", v: "$total"
}
}
}
},
{
$project: {
_id: 0,
city: "$_id",
Count: { $mergeObjects: [ { filter: "prepaid" }, { count: { $arrayToObject: "$Count" } } ] }
}
}
])
添加回答
舉報(bào)