Thursday, November 02, 2017

How to Create Case-Insensitive Indexes in MongoDB

DZone Database Zone
How to Create Case-Insensitive Indexes in MongoDB
How to Create Case-Insensitive Indexes in MongoDB

Case-insensitive indexes support queries that perform string comparisons without regard for letter case, and with MongoDB 3.4's support of Collation, these are now possible to build. Collation gives you the ability to specify language-specific rules for string comparison. Since MongoDB’s previous versions did not support Collation, you were limited to performing a case-sensitive index comparison.  In the scenarios that needed case-insensitive behavior, the only option was to convert/store all of your strings to either uppercase or lowercase and then do the comparison. As you can imagine, this causes a lot of hassle with queries and index operations.

Creating a MongoDB Case-Insensitive Index

The Collation property can be set at the collection level or explicitly when creating an index. If set at the collection level, it doesn’t need to be specified with every create-index command on, as the index inherits the Collation of the collection. Unless specified explicitly during creation time, a collection has no Collation associated with it. You can determine the Collation details of your collection using commands provided below:

No comments:

Fun With SQL: Functions in Postgres

DZone Database Zone Fun With SQL: Functions in Postgres In our previous  Fun with SQL  post on the  Citus Data  blog, we covered w...