0

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));
1
  • Sorry i think im starting to rely on SOF a little too much and getting a little post happy. After about another 2 mins of searching i found the answer. I posted it in my original post at the bottom. Commented Jan 17, 2012 at 22:45

1 Answer 1

5

I think this is a PHP syntax error:

$collection->find().sort({user_id: -1})

is invalid PHP. In PHP, the dot operator (".") is used to concatenate strings, not to access object members. For that, use the arrow operator ("->"). Additionally, "{user_id: -1}" is not correct syntax for an associative array in PHP. For that, use "array("user_id" => -1)".

This gives us:

$collection->find()->sort(array("user_id" => -1))

which I believe should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.