Prior to MongoDB version 3.4, we were unable to create a case insensitive index .
In version 3.4 there is an option collationthat allows users to specify language rules for comparing strings, such as rules for alphabetic and accent marks.
The mapping parameter has the following syntax:
collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
locale ; .
, locale strong . strength rage 1 - 5.
,
:
strong = 1, role = Role = rôle
strong = 2, role = Role < rôle
strong = 3, <
strength=2 . :
db.collectionName.createIndex(
{ name: 1, formula: 1, type: 1 },
{
name: "fertilizer_idx",
collation: {locale: "en", strength: 2},
unique: true
}
)
N.B: collation .