在mysql里面,我们偶尔会有这样的sql出现:
select userid,count(userid) as cnt from xxxxxtable group by userid;
这样的sql在mongo里怎么实现呢?其实也相对比较简单
<?php
$mongo = new Mongo();
$mongo->selectDB('xxx');//库
$collection = $mongo->selectCollection('表');
$field = array('userid'=>1);
$cnt = array('cnt'=>0);
$result = $collection->group($field,$cnt,"function(obj,pref){pref.cnt++;}");
打印出来的结果也很方便识别:
Array
(
[retval] => Array
(
[0] => Array
(
[userid] => 11111
[cnt] => 2
)
//省略
)
[count] => 21
[keys] => 11
[ok] => 1
)
果然轻轻松松啊

评论 (0)
暂无评论