简体   繁体   中英

find string containing special characters in mongodb using $regex

using $regex in mongodb, I want to find the name B&B Hôtel which contain some special characters like & and ô by typing BB Hotel .

I tried this code:

db.txt.find({ "name": {'$regex': query, $options:'i'}})

where query can be BB Hotel .

You don't want regex search, you want diacritic insensitive text search

"name":{
  $text:
    {
      $search: "\"B&B Hotel\""
      $caseSensitive: false,
      $diacriticSensitive: false
    }
}

Note that $diacriticSensitive defaults to false, but I never trust the defaults. If you are running with older indexes (version 2 or less text index), you may not be able to use the indexes. The escaped " in the search part is to search for this phrase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM