0
好吧我使用蒙戈db和有我的頁面上的重複區域:蒙戈 - 排序while循環
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});
任何幫助,將不勝感激。謝謝。
答案發現 *
$cursor = $collection->find();
$cursor->sort(array('user_id' => 1));
對不起,我想我會開始依賴SOF有點太多,並得到一個小後感到高興。經過約2分鐘的搜索後,我找到了答案。我把它貼在我原來的帖子底部。 – user982853 2012-01-17 22:45:32