How does sorting work in the new mongodb PECL exte

2019-01-12 10:18发布

问题:

I have just shifted from the old mongo extension to the newest PHP driver (mongodb). I'm also using the PHP library provided for mongodb.

Previously, I could simply do ->sort() on a cursor instance, but that doesn't seem to be the case anymore 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? Help would be appreciated.

回答1:

I got an answer about this on the respective Github repository.

To be able to sort with find, you simply use the second find parameter like this:

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

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

For more information, look into the issue I have posted at the repository:

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



标签: php mongodb