Reputation: 1464
Right now I got:
@directories = collection.directories.all.asc(:name)
But it's case-sensitive, how do I do case-insensitive sorting?
Upvotes: 11
Views: 4807
Reputation: 373
If your collection is not going to crash memory (99% of the time), just sort it there:
Blah.all.sort_by{|i| i.blah_field.downcase}
Upvotes: 2
Reputation: 318
regexsearch = Regexp.new(params[:search], true)
@users = User.where(:email => regexsearch).all
That should do it :)
Upvotes: -4
Reputation: 27080
Currently you cannot create case insensitive indexes in MongoDB see ...
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.
Eliot Horowitz from 10Gen (the supporters of MongoDB) suggest this in the meantime:
For short term - I would just add a 2nd field that you call .toLower() on before inserting. Then you can sort on that.
Upvotes: 16
Reputation: 53685
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
Upvotes: 4