为什么这种mongo排序在PHP中不起作用? [英] Why is this mongo sort not working in PHP?
本文介绍了为什么这种mongo排序在PHP中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在按照PHP文档中的示例对集合中的某些记录进行排序:
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).
但是从mongo控制台执行此查询会产生正确排序的响应:
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.
推荐答案
我认为我看到了问题.而不是这样做:
I think I see the problem. Instead of doing this:
$cursor->sort(array("score",-1))
尝试一下:
$cursor->sort(array("score" => -1))
容易犯错,但是很沮丧地发现您是否马上就看不到它.
Easy mistake to make, but very frustrating to find if you don't see it right away.
这篇关于为什么这种mongo排序在PHP中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文