<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
is what you need to use. A lot of information has been covered by searching, so I won’t go into details here. Judging from your code, it should be roughly written like this
<code>db.collection.aggregate([ {$group: {_id: {AREA: "$AREA"}, show: {$sum: "$show"}, click: {$sum: "$click"}, cost: {$sum: "$cost"}}}, {$limit: 1000} ]);</code>
The return is a cursor.