Sunday, September 1, 2019

How to Create a MongoDB Case-Insensitive Index

MongoDB added collation support in version 3.4, so that you can specify collation for a collection or a view, an index, or certain operations that support collation, such as find() and aggregate().

Collation Document Fields
To use collation options other than the default, you can specify a Collation Document. It's made up of the following fields:
{
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}


Set the Collation Property while building an index

You also have the option to explicitly set the Collation for an index while building it. For example, adding “index” on the name property of the test collection with locale ‘en’ and strength 2:
1
db.test.createIndex( { name: 1}, { collation: { locale: 'en', strength: 2 } })

No comments:

Post a Comment

Kafka setup in window

Here's a step-by-step guide on how to do it : 1. Prerequisites : Before you begin, make sure you have the following prerequisites inst...