Mongodb将带重音符号的字符作为基础字符

Mongodb将带重音符号的字符作为基础字符

本文介绍了Mongodb将带重音符号的字符作为基础字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用MongoDB"db.foo.find()"语法,如何告诉它匹配所有字母及其重音版本?

In MongoDB "db.foo.find()" syntax, how can I tell it to match all letters and their accented versions?

例如,如果我的数据库中有一个名称列表:
João
弗朗索瓦
耶苏斯

For example, if I have a list of names in my database:
João
François
Jesús

如何允许搜索字符串"Joao","Francois"或"Jesus"以匹配给定名称?
我希望我不必每次都进行这样的搜索:
db.names.find({name : /Fr[aã...][nñ][cç][all accented o characters][all accented i characters]s/ })

How would I allow a search for the strings "Joao", "Francois", or "Jesus" to match the given name?
I am hoping that I don't have to do a search like this every time:
db.names.find({name : /Fr[aã...][nñ][cç][all accented o characters][all accented i characters]s/ })

推荐答案

从Mongo 3.2开始,您可以使用$text并将$diacriticSensitive设置为false:

As of Mongo 3.2, you can use $text and set $diacriticSensitive to false:

{
  $text:
    {
      $search: <string>,
      $language: <string>,
      $caseSensitive: <boolean>,
      $diacriticSensitive: <boolean>
    }
}

在Mongo文档中查看更多信息: https://docs.mongodb. com/manual/reference/operator/query/text/

See more in the Mongo docs: https://docs.mongodb.com/manual/reference/operator/query/text/

这篇关于Mongodb将带重音符号的字符作为基础字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:42