Mongodb 匹配重音字符作为底层字符 [英] Mongodb match accented characters as underlying character

查看:14
本文介绍了Mongodb 匹配重音字符作为底层字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如,如果我的数据库中有一个姓名列表:
若昂
弗朗索瓦
耶稣

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ç][所有重音 o 字符][所有重音 i 字符]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 匹配重音字符作为底层字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆