如何使用 MongoDB PHP 扩展获取游标结果计数
How to get cursor result count using the MongoDB PHP extension
mongo
PHP extension is deprecated in favour of the mongodb
extension. This extension is used together with the mongo-php-library
.
在旧的扩展中,可以使用 MongoCursor::count(). However, the new cursor MongoDB\Driver\Cursor 从游标中获取结果计数,但没有这样的方法。在对 MongoDB 执行查询后获取结果数量的新方法是什么?
你可以这样做
Model::count(array($whereClause));
$whereClause
基本上就是您的搜索条件。
否则,如果您查询returns一个数组,您可以
$data = Model::find(array($whereClause));
$total = count($data);
我用这个代码。
$query = ["hello" => "world"];
$command = new MongoDB\Driver\Command(["count" => "collection", "query" => $query]);
try {
$result = $mongo->executeCommand("mydb", $command);
$res = current($result->toArray());
$count = $res->n;
echo $count;
} catch (MongoDB\Driver\Exception\Exception $e) {
echo $e->getMessage(), "\n";
}
mongo
PHP extension is deprecated in favour of the mongodb
extension. This extension is used together with the mongo-php-library
.
在旧的扩展中,可以使用 MongoCursor::count(). However, the new cursor MongoDB\Driver\Cursor 从游标中获取结果计数,但没有这样的方法。在对 MongoDB 执行查询后获取结果数量的新方法是什么?
你可以这样做
Model::count(array($whereClause));
$whereClause
基本上就是您的搜索条件。
否则,如果您查询returns一个数组,您可以
$data = Model::find(array($whereClause));
$total = count($data);
我用这个代码。
$query = ["hello" => "world"];
$command = new MongoDB\Driver\Command(["count" => "collection", "query" => $query]);
try {
$result = $mongo->executeCommand("mydb", $command);
$res = current($result->toArray());
$count = $res->n;
echo $count;
} catch (MongoDB\Driver\Exception\Exception $e) {
echo $e->getMessage(), "\n";
}