Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 578
Icid Avatar asked Dec 20 '10 16:12

Icid


People also ask

Are MongoDB field names case sensitive?

Database names and Collection names are case sensitive. You can always recreate the DB/Collection with the appropriate name.

Which type of sorting is supported by MongoDB?

MongoDB can perform sort operations on a single-field index in ascending or descending order. In compound indexes, the sort order determines whether the index can be sorted. The sort keys must be listed in the same order as defined in the index.

How do I sort alphabetically in MongoDB?

To sort documents in MongoDB, you need to use sort() method. The method accepts a document containing a list of fields along with their sorting order. To specify sorting order 1 and -1 are used. 1 is used for ascending order while -1 is used for descending order.


1 Answers

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.

like image 158
Justin Jenkins Avatar answered Oct 10 '22 00:10

Justin Jenkins