How does sorting work in the new pecl mongodb extension?

I just switched from the old Mongo extension to the latest PHP driver (mongodb). I also use the PHP library provided for mongodb.

Previously, I could just do ->sort()for the cursor instance, but this is no longer the case since I get this error:

Fatal error: Uncaught Error: Call to undefined method MongoDB\Driver\Cursor::sort()

What is the alternative to sorting / limiting / skipping now?

+4
source share
1 answer

I got an answer about this in the corresponding Github repository .

To be able to sort with find, you simply use the second parameter findas follows:

$filter  = [];
$options = ['sort' => ['username' => 1]];

$client = new MongoDB\Client('mongodb://localhost');
$client->mydb->mycollection->find($filter, $options);

, :

https://github.com/mongodb/mongo-php-driver/issues/214

+11

Source: https://habr.com/ru/post/1626973/


All Articles