Maison >développement back-end >tutoriel php >mongodb再执行group之后,取一千条数据,再怎么limit操作,代码如下面描述?
<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。