Why is this mongo sort not working in PHP?

mongodb, php

Solution

I think I see the problem. Instead of doing this:

$cursor->sort(array("score",-1))

Try this:

$cursor->sort(array("score" => -1))

Easy mistake to make, but very frustrating to find if you don't see it right away.

Problem

I'm following an example from the PHP docs to sort some records in a collection: ``` $cursor = $mongo->party_scores->find()->limit(10); $cursor = $cursor->sort(array("score",-1)); foreach($cursor as $doc) { print_r($doc); } ``` Doing this, I see the documents in a random order (not sorted). But executing this query from the mongo console produces a correctly sorted response: ``` db.party_scores.find().sort({score : -1 }) ``` I feel like there must be something obvious I'm missing.

Original source