我正在嘗試聚合本文檔中的數據包總數。Mongodb是否可以聚合一個對象?
{
"_id" : ObjectId("51a6cd102769c63e65061bda"),
"capture" : "1369885967",
"packets" : {
"0" : "595",
"1" : "596",
"2" : "595",
"3" : "595",
...
}
}
我能得到的最接近的是約
db.collection.aggregate({ $match: { capture : "1369885967" } }, {$group: { _id:null, sum: {$sum:"$packets"}}});
但是它返回總和0,這顯然是錯誤的。
{ "result" : [ { "_id" : null, "sum" : 0 } ], "ok" : 1 }
如何獲得所有數據包的總和?
你需要'$ unwind'這個'packets'子文檔,你也需要把這個子文檔改成一個數組 – Sammaye
不可能是一個對象嗎?如果是這種情況,什麼時候使用對象是合適的? – resting
啊,你知道你得到的對象....嗯....否 – Sammaye