0

I'm trying to create case sensitive index with mongoDB version 3.4? I'm using the following query to create an index but still it allows me to insert data with different case?

db.Test.createIndex( { "type" : 1 },{ unique: true , collation: { locale: 'en' ,caseLevel:true ,strength: 3 } } )

in the above query i'm making Type as unique. First i inserted "apple" into the database and when i try to "apple" it throws duplicate error. but when i try to insert "Apple" it allows me to insert. for me while inserting "Apple" it should throws duplicate error.

  • Well the [option](https://docs.mongodb.com/manual/reference/collation/#collation-document-fields) is under the word ["collation"](https://en.wikipedia.org/wiki/Collation) which basically does not have anything to do with uniqueness, and is merely used for "sorting". If it was not clear enough. There is no way to make an index "unique" on case. MongoDB does not support that. That's up to you instead. The links there are both to the manual and the english definition of "collation", where that was not understood. – Neil Lunn Jun 28 '17 at 14:17
  • See also the issue the creation of the feature actually addresses https://jira.mongodb.org/browse/SERVER-90 – Neil Lunn Jun 28 '17 at 14:20
  • Also for reference: All the answers to this question are therefore incorrect: https://stackoverflow.com/questions/33736192/mongo-unique-index-case-insensitive and all misinterpret the function. – Neil Lunn Jun 28 '17 at 14:23

1 Answers1

0

Strength 2 will work

db.Test.createIndex({  
   type:1
},
{  
   collation:{  
      locale:"en",
      strength:2
   },
   unique:true
}));
Naga Penmetsa
  • 384
  • 5
  • 16