Reputation: 4541
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 any more 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?
Upvotes: 7
Views: 4000
Reputation: 4541
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
Upvotes: 20