我刚刚从旧的Mongo扩展转移到最新的PHP驱动程序(mongodb)。我也在使用为 mongodb 提供的 PHP 库。
以前,我可以简单地在游标实例上执行->sort()
,但自从我收到此错误以来,
Fatal error: Uncaught Error: Call to undefined method MongoDB'Driver'Cursor::sort()
现在排序/限制/跳过的替代方案是什么?
我在各自的Github存储库上得到了答案。
为了能够使用 find
进行排序,您只需使用第二个find
参数,如下所示:
$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