本文介绍了在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" }

此查询将找到两条记录

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中使用归类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:58