好的,我正在使用mongo,并且在我的页面上有一个重复区域:
try {
$connection = new Mongo();
$database = $connection->selectDB($selectDB);
$collection = $database->selectCollection($selectCollection);
} catch(MongoConnectionException $e) {
die("Failed to connect to database ".$e->getMessage());
}
$cursor = $collection->find();
while ($cursor->hasNext()): $document = $cursor->getNext();
echo $document['fieldName']."<br/>";
echo $document['fieldType']."<br/>";
echo $document['fieldLength']."<br/>";
echo $document['user_id']."<br/>";
echo $document['order']."<br/>";
echo "<hr/>";
endwhile;这很好,但我现在要做的是按user_id排序。我试过这样做:
try {
$connection = new Mongo();
$database = $connection->selectDB($selectDB);
$collection = $database->selectCollection($selectCollection);
} catch(MongoConnectionException $e) {
die("Failed to connect to database ".$e->getMessage());
}
$cursor = $collection->find().sort({user_id: -1});
while ($cursor->hasNext()): $document = $cursor->getNext();
echo $document['fieldName']."<br/>";
echo $document['fieldType']."<br/>";
echo $document['fieldLength']."<br/>";
echo $document['user_id']."<br/>";
echo $document['order']."<br/>";
echo "<hr/>";
endwhile;我修改的行是:$cursor = $collection->find().sort({user_id:-1});
我得到了php错误时,id o这。有人能告诉我正确的syntex让这个数组排序吗?
我也尝试过:
$cursor = $collection->find()->sort({user_id: -1});
and
$cursor = $collection->find();
$cursor = $cursor.sort({user_id: -1});任何帮助都将不胜感激。谢谢。
found* 应答
$cursor = $collection->find();
$cursor->sort(array('user_id' => 1));发布于 2012-01-17 22:52:08
我认为这是PHP语法错误:
$collection->find().sort({user_id: -1})无效的PHP。在PHP中,点运算符(".")用于连接字符串,而不是访问对象成员。为此,使用箭头操作符("->")。此外,"{user_id: -1}“对于PHP中的关联数组来说是不正确的语法。为此,请使用"array("user_id" => -1)“。
这使我们:
$collection->find()->sort(array("user_id" => -1))我认为这应该有效。
https://stackoverflow.com/questions/8902693
复制相似问题