1);$initial = array( "show" =>0, "click"=>0, "cost"=>0, );$reduce = "function (o">
时间:2021-07-01 10:21:17 帮助过:85人阅读
$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);
$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);
aggregation
是你需要使用的东西。搜一下已经挺多讲,这里不细说了。从你的代码看应该大致是这样的写法
db.collection.aggregate([
{$group: {_id: {AREA: "$AREA"}, show: {$sum: "$show"}, click: {$sum: "$click"}, cost: {$sum: "$cost"}}},
{$limit: 1000}
]);
返回是一个cursor。