我有一個名爲test
的集合。我希望獲得test
集合中存在的所有文檔的總金額:sal
。
下面是示例文件:
[
{
"name": "hari",
"sal": "100",
"status": "Y",
"address": "Bangalore"
},
{
"name": "Sam",
"sal": "200",
"status": "Y",
"address": "Bangalore"
},
{
"name": "Nik",
"sal": "200",
"status": "N",
"address": "Bangalore"
}
]
,我嘗試以下查詢:
db.getCollection('test').aggregate([{
$match: {
Status: "Y"
}
}, {
$group: {
_id: null,
total: {
$sum: "$sal"
}
}
}])
我越來越爲零。
任何人都可以給我建議的權利查詢到所有的文件集合中添加特定的鍵。
可能重複[如何在MongoDB中總金額獲得總計數?(http://stackoverflow.com/questions/17044587/how-to-aggregate-sum-in-mongodb-to-get-a-total-count) – Yogesh
這聽起來像你'SAL '字段實際上是一個字符串,或者至少不是一個數字,或者不存在。 –
您的查詢乍一看看起來很正確,但可能不適合您的文檔。請發佈示例文檔。 – Philipp