简体   繁体   English

PHP MongoDB按字母顺序对结果进行排序

[英]PHP MongoDB sort results by alphabetical

I'm sure this is really simple to do, but how do you sort by alphabetical (preferably a natural sort) in mongo w/ PHP? 我确信这确实很简单,但是如何在w / PHP中按字母顺序(最好是自然排序)进行排序? This is what I have right now: 这就是我现在所拥有的:

$data->sort(array('title' => 1));

And it's not returning the array in alphabetical order. 而且它不是按字母顺序返回数组。 Am I missing something?! 我错过了什么吗?

actually this should sort in A-Za-z order, which is related to how MongoDB works with the indexes. 实际上,这应该按照A-Za-z的顺序排序,这与MongoDB如何处理索引有关。 You can have a look at the ticket: https://jira.mongodb.org/browse/SERVER-90 which is about the indexing issue. 您可以看一下票证: https : //jira.mongodb.org/browse/SERVER-90关于索引问题。 (see: https://jira.mongodb.org/browse/SERVER-105 for the specific ticket, which just references back to 90). (有关特定票证,请参阅: https//jira.mongodb.org/browse/SERVER-105 ,该票证仅回溯至90)。

In the code example Eliot suggests to do something like: { name : { real : "Eliot" , sort : "eliot" } } which basically means you have a second field which you use only for the sorting, that contains the string you want to sort by all lowercase. 在代码示例中,Eliot建议执行类似的操作:{name:{real:“ Eliot”,sort:“ eliot”}}}这基本上意味着您还有第二个字段,仅用于排序,其中包含所需的字符串按所有小写字母排序。

Sadly, until this ticket is fixed all solutions are just workarrounds, but Eliots solution allows you to sort and only retrieve a part of the set, opposed to retrieving the whole set and sorting by php 可悲的是,在此票证修复之前,所有解决方案都只是工作日,而Eliots解决方案允许您排序并仅检索集合的一部分,而不是检索整个集合并通过php进行排序

I am new to mongodb. 我是mongodb的新手。 But from what I understand about it is that less work should be done in mongodb and more work should be done in application. 但是据我了解,在mongodb中应减少工作量,而在应用程序中应进行更多工作。 Its why mongodb can hit high speeds, simply because it is not trying to be an application its self like an RDMS. 这就是为什么mongodb能够达到高速的原因,仅仅是因为它并没有像RDMS那样成为自己的应用程序。 I think you will need to index the collection tho. 我认为您将需要索引该集合。 just use ensureIndex(array('title' => 1)); 只需使用sureIndex(array('title'=> 1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM