我有以下运行的PHP代码:
global $m, $db;
$collection = $db->locations;
try{
$cursor = $collection->find();
return $cursor;
} catch(MongoCursorException $e) {
return false;
}
现在我想通过名为" name"的字段按升序对结果进行排序。
这是我尝试过的:
global $m, $db;
$collection = $db->locations;
try{
$cursor = $collection->find().sort({name:1});
return $cursor;
} catch(MongoCursorException $e) {
return false;
}
但我收到的错误是:
Parse error: syntax error, unexpected '{' in /var/www/html/myapp/models/locations_model.php on line 23
任何提示?
答案 0 :(得分:0)
尝试
$cursor = $collection->find()->sort(['name' => 1]);
您的错误是因为您没有将数组发送到它所期望的排序,请参阅PHP mongocursor sort
答案 1 :(得分:0)
Try
$cursor = $collection->find()->sort(array('name' => 1));
更多细节Click here。