集合存在如下:如何根據mongodb中的值獲取多個字段的計數?
[
{"currentLocation": "Chennai", "baseLocation": "Bengaluru"},
{"currentLocation": "Chennai", "baseLocation": "Bengaluru"},
{"currentLocation": "Delhi", "baseLocation": "Bengaluru"},
{"currentLocation": "Chennai", "baseLocation": "Chennai"}
]
預期輸出:
[
{"city": "Chennai", "currentLocationCount": 3, "baseLocationCount": 1},
{"city": "Bengaluru", "currentLocationCount": 0, "baseLocationCount": 3},
{"city": "Delhi", "currentLocationCount": 1, "baseLocationCount": 0}
]
我曾嘗試是:
db.getCollection('users').aggregate([{
$group: {
"_id": "$baselocation",
baseLocationCount: {
$sum: 1
}
},
}, {
$project: {
"_id": 0,
"city": "$_id",
"baseLocationCount": 1
}
}])
得到結果爲:
[
{"city": "Chennai", "baseLocationCount": 1},
{"city": "Bengaluru", "baseLocationCount": "3"}
]
我我不是家人我和蒙戈在一起,有什麼幫助嗎?
MongoDB的版本 - 3.4
讓我們知道你在這裏嘗試 – sidgate
覈對答案:https://開頭stackoverflow.com/questions/45307580/group-by-day-with-multiple-date-fields/45312816#45312816 – dnickless