use indbar1
db.branch.insert({ "name" : "b1" , "y2000" : 80 , "y2001" : 105 })上面給出的是數據庫和集合名稱的剪輯以及數據類型的外觀
1 回答

三國紛爭
TA貢獻1804條經驗 獲得超7個贊
您可以嘗試以下聚合查詢:
db.collection.aggregate([
/** Match docs where both fields exists */
{
$match: {
"y2000": {
$exists: true
},
"y2001": {
$exists: true
}
}
},
/** Group branches & sum up each fields values across all docs */
{
$group: {
_id: "$name",
y2000: {
$sum: "$y2000"
},
y2001: {
$sum: "$y2001"
}
}
},
/** Transform fields to required format */
{
$project: {
_id: 0,
x: "$_id",
y2000: 1,
y2001: 1
}
}
])
測試 : 蒙哥德-游樂場
添加回答
舉報
0/150
提交
取消