简体   繁体   English

使用Mongoid进行不区分大小写的排序

[英]Case insensitive sorting with Mongoid

Right now I got: 现在我得到了:

@directories = collection.directories.all.asc(:name)

But it's case-sensitive, how do I do case-insensitive sorting? 但它区分大小写,我如何进行不区分大小写的排序?

Currently you cannot create case insensitive indexes in MongoDB see ... 目前你无法在MongoDB中创建不区分大小写的索引,请参阅...

http://jira.mongodb.org/browse/SERVER-90 http://jira.mongodb.org/browse/SERVER-90

So, it seems that means you cannot do case insensitive "sorting" either. 所以,这似乎意味着你不能做不区分大小写的“排序”。

You can upvote the feature for future inclusion in MongoDB via the link above if you find it useful. 如果您发现它有用,您可以通过上面的链接提交该功能,以便将来包含在MongoDB中。

Eliot Horowitz from 10Gen (the supporters of MongoDB) suggest this in the meantime: 来自10Gen的Eliot Horowitz(MongoDB的支持者)在此期间建议:

For short term - I would just add a 2nd field that you call .toLower() on before inserting. 短期内 - 我会在插入之前添加一个你调用.toLower()的第二个字段。 Then you can sort on that. 然后你可以对此进行排序。

You will probably have to store the field twice, once with its real value, and again in all lowercase. 您可能必须将字段存储两次,一次使用其实际值,再次以全小写存储。 You can then query the lowercased version for case-insensitive search (don't forget to also lowercase the query string). 然后,您可以查询小写版本以进行不区分大小写的搜索(不要忘记同样小写查询字符串)。

This approach works (or is necessary) for many database systems, and it should perform better than regular expression based techniques (at least for prefix or exact matching). 这种方法对许多数据库系统起作用(或必要),并且它应该比基于正则表达式的技术(至少对于前缀或精确匹配)执行得更好。

Check this answer 检查这个答案

如果您的集合不会崩溃内存(99%的时间),只需在那里排序:

Blah.all.sort_by{|i| i.blah_field.downcase}

I think you will have to use regex 我想你将不得不使用正则表达式

Edit: maybe not, the feature has been requested though https://jira.mongodb.org/browse/SERVER-90 编辑:也许不是,通过https://jira.mongodb.org/browse/SERVER-90请求了该功能

regexsearch = Regexp.new(params[:search], true)

@users = User.where(:email => regexsearch).all

That should do it :) 应该这样做:)

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

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