1);$initial = array( "show" =>0, "click"=>0, "cost"=>0, );$reduce = "function (o">
当前位置:Gxlcms > PHP教程 > mongodb再执行group之后,取一千条数据,再怎么limit操作,代码如下面描述?

mongodb再执行group之后,取一千条数据,再怎么limit操作,代码如下面描述?

时间: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。

人气教程排行