PHP的MongoDB driver怎样像mysql一样count(*)统计文档条数

这是查询数据,分页查询数据没问题,就是不知道怎么查询总数
如果有像mysql中的count(*)就好了

php文档
http://php.net/manual/zh/set....

$filter = array(
    'age' => ['$gt' => 50]
);
$options = [
    'limit' => 20,
    'skip' => 10
];
// 查询数据
$query = new \MongoDB\Driver\Query($filter, $options);
$cursor = $this->manager->executeQuery($this->dbname . '.' . $collection, $query);
$rows = $cursor->toArray();
阅读 9.9k
5 个回答
$command = new MongoDB\Driver\Command(["count" => "age", "query" => $filter]);
$result = $mongo->executeCommand("db", $command);
$res = current($result->toArray());
$count = $res->n;
echo $count;

https://github.com/mongodb/mo...

推荐问题