Mongo unique case insensitive index

@CompoundIndexes({
    @CompoundIndex(name = "fertilizer_idx",
        unique = true,
        def = "{'name': 1, 'formula': 1, 'type': 1}")
})
public class Fertilizer extends Element implements Serializable {
//class stuff
}

Is it possible to create an index case insensitive? Now it is different from NAMEbefore NAME. Saving a second lower case field (or upper case) is not possible for me.

Thanks Pedro

+4
source share
3 answers

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 .

+4

, MongoDB 3.4 .

, , :

db.collection.createIndex({
   name:1,
   formula:1,
   type:1
},
{
   collation:{
      locale:"en",
      strength:2
   }
});

-

:

db.collection.find({name: "name"}).collation({locale: "en", strength: 2});

. collation

mongodb 3.4 , ,

db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } )
+1
db.collection.createIndex(
{ name: 1, formula: 1, type: 1 },
{ name: "fertilizer_idx", unique: true, collation:{ locale: "en", strength: 2 } }
)

db.collection.createIndex()

:   https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/

/: https://docs.mongodb.com/manual/reference/collation-locales-defaults/#collation-languages-locales

strength: integer

. . :

1: . Collation , , .

2: . , . ( ) ( ). .

3: . , case letter. ( ), ( ) ( ). , . .

4: . , , 1-3 .

5: . .

Mongo 3.4 ,

:

collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}
+1
source

Source: https://habr.com/ru/post/1616053/


All Articles