mongodb $regex 中排序规则的使用 [英] Use of collation in mongodb $regex

查看:8
本文介绍了mongodb $regex 中排序规则的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 v3.4 排序规则可用于查找操作,尤其是在关注变音符号的匹配时.虽然具有确定值($eq opeartor 或相应构造)的查找查询将匹配字母和对应的变音符号,但如果使用 $regex 来实现对部分搜索字符串(LIKE")的匹配,则情况并非如此).

Since v3.4 collations are available for find operations, especially as matches for diacritic characters are concerned. While a find query with a definite value ($eq opeartor or corresponding construct) will match letters and correspondent diacritics, the same is not true if a $regex is used in order to achieve a match on a partial search string (a 'LIKE').

是否可以让 $regex 查询以与 $eq 查询相同的方式使用排序规则?

Is there a to make the $regex query use the collation the same way than the $eq query?

考虑示例集合 testcoll:

consider example collection testcoll:

{ "_id" : ObjectId("586b7a0163aff45945462bea"), "city" : "Antwerpen" }, 
{ "_id" : ObjectId("586b7a0663aff45945462beb"), "city" : "Antwërpen" }

这个查询会找到两条记录

this query will find both records

db.testcoll.find({city: 'antwerpen'}).collation({"locale" : "en_US", "strength" : 1});

使用正则表达式的相同查询不会(仅查找带有Antwerpen"的记录)

the same query using a regex will not (finds the record with 'Antwerpen' only)

db.testcoll.find({city: /antwe/i}).collation({"locale" : "en_US", "strength" : 1});

推荐答案

文档

不区分大小写的正则表达式查询通常不能有效地使用索引.$regex 实现不支持排序规则,也无法使用不区分大小写的索引.

Case insensitive regular expression queries generally cannot use indexes effectively. The $regex implementation is not collation-aware and is unable to utilize case-insensitive indexes.

这篇关于mongodb $regex 中排序规则的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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