How to achieve Sort Case In-Sensitive on text field?

  • Just Completed topic on $regex. In case of sort, how can ignore character casing and sort a a text field with case in-sensitive, as in SQL.

The following will help you.

https://docs.mongodb.com/manual/core/index-case-insensitive/
https://docs.mongodb.com/manual/reference/collation/
https://docs.mongodb.com/manual/reference/operator/query/regex/

2 Likes

Hi @VINAYAKA_05231,

I hope you found the link shared by @steevej-1495 helpful.

You can use collation for this. Something like this :

db.collection.find().collation({'locale':'en'}).sort({a:-1}).pretty()

Hope it helps!

Please let me know if you have any other query.

Thanks,
Shubham Rajan
Curriculum Support Engineer

1 Like

{‘locale’:‘en’} is fine, but If are supporting multiple language content in a column then mentioning only ‘en’ will be limitation right?

is it good to have index-case-insensitive for all text/string columns in a n-number of collection? It will not affect performance?

As for performance, it always depends of your use-case.

If the majority of your finds are case insensitive, it is probably more efficient to have your index insensitive. While it might be better to have a normal index when only few finds are case insensitive. Only profiling of your application can give you a definitive answer.