Use $ diacriticSensitive with match in Mongo


I am trying to perform a search in a mongo document that has the energy word saved in the name, I make the query in the following way:

    {'$match': {
             'name': {
             '$regex': '.*energetico.*',
             '$options': 'i'

But I do not get results until I put the accent, look in Mongo's documentation and $ diacriticSensitive appeared, but when I put it in my code I get the error:         'unknown operator: $ diacriticSensitive'

Is there any way to perform the search regardless of whether you have an accent so that I can return a result?

asked by FrancJJL 13.12.2018 в 22:58

1 answer


Something similar happened to me but in my case I just needed to look for a word without counting the accents on a text, in my case I solved it using the '$ text' method of MongoDB. I show you an example.

Assuming that we have a collection 'populations' and we want to look for a word about the fields of 'province' and 'population' we would have to make the following query:

db.getCollection('poblaciones').find({$text: {$search: "almeria", $diacriticSensitive: false}})

And for MongoDD to know what field to look for we would have to create an index with the search fields:

{ "poblacion": "text", "provincia": "text"}

Links of interest:


Mongoose text search with diacriticSensitive

I hope that with this info you can solve your problem.


answered by 14.12.2018 в 12:19