Ok i am using mongo db and have a repeating region on my page:
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;
this works fine but what i am trying to do now is sort by user_id. I have tried this:
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;
The line i altered is the: $cursor = $collection->find().sort({user_id: -1});
i am getting php error when id o this. Can someone show me the proper syntex to get this array to sort.
I have also tried:
$cursor = $collection->find()->sort({user_id: -1});
and
$cursor = $collection->find();
$cursor = $cursor.sort({user_id: -1});
Any help would be appreciated. thanks.
answer found*
$cursor = $collection->find();
$cursor->sort(array('user_id' => 1));