mongodb使用distinct统计

浏览:8607 发布日期:2014/11/20 分类:技术分享 关键字: mongodb,distinct,count
因为thinkphp的mongo驱动不支持distinct关键字,使用mongocode总是报错,所以,自己实践出来使用command()方法,统计不重复字段的方法,放上实例.
thinkphp版本3.22
在model中方法 public function total($sql){
     return $this->command($sql);//返回结果请自己打印
 }
controller中调用(我使用的是controller调用api的接口,你可以自己调用)    private function _num(){
        $api = new ActionApi();
        $boy = array("sex"=>"1","uid"=>array('$in'=>$people));//$people是一个数组这是一个in条件
        $girl = array("sex"=>"2");
        $country = array("country"=>"cn");
        $sql_arr = array ('$or' =>array ($boy,$girl,$country));//使用or条件
        $all = array("distinct"=>"action","key"=>"uid","query"=>$sql_arr);//去重和组合其他方法查询
        $res = $api->total($all);
        return $res;
    }
其中使用了去重and(条件1or条件2or条件3)
生成语句为
command:{"distinct":"people","key":"uid","query":{"$or":[{"sex":"1","uid":{"$in":["2","6"]}},{"sex":"2"},{"country":"cn"}]}}
"distinct":"people"//people表
"key":"uid"//uid去重
最佳答案
评论( 相关
后面还有条评论,点击查看>>