I'm having an issue trying to sort after a map reduce. The collection has statistics (like scores) with users attached, so it's finding the highest score of the user. That portion works, the sort is to then sort those results for a leaderboard. I've put these functions into javascript variables and run them in the mongo console and a simple find().sort({'value.max.value':-1}) works fine on the resulting collection but I can't get it to work here. (My results come back unordered).
$query->map('function() {
var x = { value : parseInt(this.value), _id : this._id, date : this.postDate };
emit(this.user, { max : x });
}')
->reduce('function(key, vals) {
var res = vals[0];
for (var i=1; i<vals.length; i++)
{
if(vals[i].max.value > res.max.value)
res.max = vals[i].max;
}
return res;
}')
->sort('value.max.value', 'desc');