Hello @Michael_Sudnik, welcome to the MongoDB Community forum.
You can use Collation to perform case and diacritic insensitive search on a single field. For example,
db.test.find()
{ "_id" : 1, "a" : "café" }
{ "_id" : 2, "a" : "Café" }
{ "_id" : 3, "a" : "Cafe" }
{ "_id" : 4, "a" : "cafe" }
db.test.find({ a: "cafe" }).collation( { locale: "en_US", strength: 1 } )
// returns all the four documents
Note that in the above query, the document field is not indexed.
You can also specify collation as an option when creating an index on a field (see Create Index - Collation as an Option.
- If you do specify a collation when creating the index, MongoDB creates the index with the specified collation.
- By specifying a collation
strength
of 1
or 2
, you can create a case-insensitive index. Index with a collation strength
of 1
is both diacritic- and case-insensitive.
- IMPORTANT: To use an index for string comparisons, an operation must also specify the same collation.
I think you cannot use case and diacritic insensitive search using a Regex search. You can only do a case-insensitive search. In case you need to do a search with both options, you can specify the query like this, for example:
db.test.find({ a: { $regex: "caf[eé]", $options: "i" }})
This matches documents like the following:
{ "_id" : 4, "a" : "cafe" }
{ "_id" : 3, "a" : "Cafe caffe" }
{ "_id" : 1, "a" : "café x" }
{ "_id" : 2, "a" : "y Café" }