0

I've tried a number of variations to get mongodb php to sort by score decending. It won't do it.

here's my script

  $dd=array('gameID'=>(int)$gameID);
  $s=array( 'score' => -1);
  $cursor = $collectiontsWon->find($dd)->sort($s)->limit(7)->skip(0);

Everything is right, this is like the 8 variation of sort I've used. But it's NOT sorting by score decending on my site. What am I doing wrong?

3
  • Can you provide a sample document? Also have you tried querying directly via the CLI? Commented Mar 16, 2014 at 16:35
  • I had a screen shot and link but that got deleted Commented Mar 16, 2014 at 17:45
  • I saw that but it did not contain any useful information for your problem nor a sample document from your database. It would be useful to present one of those mongodb documents you are trying to sort and see if we can spot any error with it. Also what about the CLI I asked about? Commented Mar 16, 2014 at 17:58

2 Answers 2

1

Try setting the cursor, then sorting the data

$cursor = $mongo->collection->find()->limit(7);

$cursor = $cursor->sort(array("score" => -1));
foreach($cursor as $doc) {
    // Do something...
}

Also, I don't think you need to explicitly set skip to 0.

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

Comments

0

<?php

$cursor->sort(array('x' => 1)); // Sort on field x, ascending

// The order in the associative array is important. For instance, these two // examples will yield different results:

$cursor->sort(array('date' => 1, 'age' => -1)); // Sort on date ascending and age descending

$cursor->sort(array('age' => -1, 'date' => 1)); // Sort on age descending and date ascending

?>

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.