代码如下,试了多种写法,要么报错什么对数不对,要么语法错误
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();
} 最佳答案