TP5.1使用aggregate统计数据一直报错

浏览:727 发布日期:2018/11/04 分类:ThinkPHP5专区 关键字: aggregate 聚合查询
求助,我想做一个统计查询,可是一直没写对,大神帮忙看一下应该要怎么写
代码如下,试了多种写法,要么报错什么对数不对,要么语法错误public function counts(){
        
        $where[] = ['imei','=','868575026638330'];
        $where[] = ['datetime','between',[1541260800,1541347200]];
        $where[] = ['clientmac','<>',''];
        /*
        $where_mongo = [
            "aggregate"=>"Yanndata",
            "pipeline" =>[
            ['$match'=>(object)$where],['$group'=>['_id'=>'$clientmac','count'=>['$sum'=>1]]],]
        ];
        
        $list = Db::table('Yanndata')->where($where_mongo)->select();
        */
        /*
        $query = [
            "aggregate"=>"Yanndata",
            "pipeline" =>[
                [
                    '$match' => $where,
                ],
                [
                    '$group' => [
                        "_id" => '$clientmac',
                        'count'=>['$sum'=>1],
                    ],
                ],
            ]
        ];
        $sql = new Command($query);
        $list = Db::command($sql,'yanndb');
        */
        

        $config = [
                // 数据库类型
                'type'           => '\think\mongo\Connection',
                // 设置查询类
                'query'          => '\think\mongo\Query',
                // 服务器地址
                'hostname'       => '127.0.0.1',
                // 集合名
                'database'       => 'yanndb',
                // 用户名
                'username'       => '',
                // 密码
                'password'       => '',
                // 端口
                'hostport'       => '',
                // 数据库调试模式
                'debug'           => true,
        ];
        
        $db = $db = Db::connect($config)->name('Yanndata');
        
        $query = array(
            'aggregate' => 'Yanndata',
            'pipeline' => array(
                array('$match' => array(
                    array('imei'=>'868575026638330'),
                )),
                array('$group' => array(
                    '_id' => '$clientmac',
                    'total' => array(
                        '$sum' => 1
                    ),
                )),
            ),
        );
        
        $sql = new Command($query);
        $list = $db->command($sql);
        var_dump($list);
        
        
        //db.Yanndata.aggregate([{$match:{imei:'868575026638330',datetime:{$gt:1541260800,$lt:1541347200},clientmac:{$ne:''}}},{$group:{_id:"$clientmac",count:{$sum:1}}},{$sort:{count:-1}},{$limit:10}])
        //上面这句是直接在mongo里执行得到正确结果的语句
        return view();
    }
最佳答案
评论( 相关
后面还有条评论,点击查看>>