Use $ diacriticSensitive with match in Mongo

0

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
source

1 answer

0

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:

MongoDB

Mongoose text search with diacriticSensitive

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

Greetings

    
answered by 14.12.2018 в 12:19