Xatoo Xatoo - 7 months ago 84
PHP Question

How to get cursor result count using the MongoDB PHP extension

The

PHP extension is deprecated in favour of the
mongodb
extension. This extension is used together with the
mongo-php-library
.

In the old extension one could get the result count from the cursor using MongoCursor::count(). However, the new cursor MongoDB\Driver\Cursor has no such method. What is the new way of getting the number of results from after performing a query against MongoDB?

Answer

You can do it like this

Model::count(array($whereClause));

The $whereClause would be your search criteria basically.

Otherwise, if your query returns an array, you could do

$data = Model::find(array($whereClause));
$total = count($data);