<code>$keys = array( "AREA" => 1 ); $initial = array( "show" =>0, "click"=>0, "cost"=>0, ); $reduce = "function (obj,prev) { prev.show += obj.show; prev.click += obj.click; prev.cost +=obj.cost; } "; $re = $collection->group($keys, $initial, $reduce);</code>
<code>$keys = array( "AREA" => 1 ); $initial = array( "show" =>0, "click"=>0, "cost"=>0, ); $reduce = "function (obj,prev) { prev.show += obj.show; prev.click += obj.click; prev.cost +=obj.cost; } "; $re = $collection->group($keys, $initial, $reduce);</code>
aggregation
是你需要使用的東西。搜一下已經挺多講,這裡不細說了。從你的程式碼看應該大致是這樣的寫法
<code>db.collection.aggregate([ {$group: {_id: {AREA: "$AREA"}, show: {$sum: "$show"}, click: {$sum: "$click"}, cost: {$sum: "$cost"}}}, {$limit: 1000} ]);</code>
回傳是一個cursor。