我已经在php 5.6中完成了计数查询
$start_time = new MongoDate(strtotime('2018-09-15 00:00:00'));
$end_time = new MongoDate(strtotime('2018-09-20 23:59:59'));
$m = new Mongo("mongodb://localhost:27017");
$mongodb = $m->adplaydsp;
$query['created_at'] = array('$gte' => $start_time, '$lte' => $end_time);
$total_request = $mongodb->bid_request->find($query)->timeout(-1)->count();
但是当我用PHP 7语法重写代码时,我无法按日期计数
$start_time = new MongoDB\BSON\UTCDateTime((new DateTime('2018-09-15 00:00:00')));
$end_time = new MongoDB\BSON\UTCDateTime((new DateTime('2018-09-20 23:59:59')));
$manager = new \MongoDB\Driver\Manager("mongodb://localhost:27017");
$reqCommand = new \MongoDB\Driver\Command(["count" => "bid_request", "query" => ['gte' => $start_time, 'lte' => $end_time]]);
$reqCount = $manager->executeCommand('adplaydsp', $reqCommand);
$reqTotal = $reqCount->toArray();
答案 0 :(得分:0)
我错过了命令中的日期字段参数。下面的解决方案
$manager = new \MongoDB\Driver\Manager("mongodb://localhost:27017");
$reqCommand = new \MongoDB\Driver\Command(["count" => "bid_request", "query" => ['created_at' => ['$gte' => $start_time, '$lte' => $end_time]]]);
$reqCount = $manager->executeCommand('adplaydsp', $reqCommand);
$total_request = $reqCount->toArray()[0]->n;