0

This is how I created unique index for my key

db.members.createIndex( { "name": 1 }, { unique: true } )

but it does accept "name" : "Alex" and "name" : "alex"

is there a way to provide case sensitive and make sure there is only one alex in this case? Thanks!

KevinVuD
  • 581
  • 2
  • 5
  • 25
  • Despite someone copying from the duplicate answer it's actually `strength: 2` as generally the better option for common use. And in the documentation - [Create a Case Insensitive Index](https://docs.mongodb.com/manual/core/index-case-insensitive/#create-a-case-insensitive-index) – Neil Lunn Jun 09 '18 at 12:00
  • yes, strength 1 will allow collation matching of characters in different accents. kiddo = kiddø – kiddorails Jun 09 '18 at 12:10
  • I tested strength 2 and mongoDb won't accept Alex1 eventhough it is not the same as alex. Any thoughts? – KevinVuD Jun 09 '18 at 12:12

1 Answers1

2

If you're using mongodb which version >= 3.4, you can use collation option:

db.myColl.createIndex( { category: 1 }, { unique: true, collation: { strength: 1 } } )
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51