I have a very annoying problem with a case insensitive query on mongodb.
我对mongodb上的一个不区分大小写的查询有一个非常烦人的问题。
I'm using MongoTemplate in a web application and I need to execute case insensitive queries on a collection.
我在Web应用程序中使用MongoTemplate,我需要对集合执行不区分大小写的查询。
with this code
用这个代码
Query q = new Query();
q.addCriteria(Criteria.where("myField")
.regex(Pattern.compile(fieldValue, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)));
return mongoTemplate.findOne(q,MyClass.class);
I create the following query
我创建以下查询
{ "myField" : { "$regex" : "field value" , "$options" : "iu"}}
that works perfectly when I have simple text, for example:
当我有简单的文字时,它可以很好地工作,例如:
caPITella CapitatA
caPITella CapitatA
but...but...when there are parenthesis () the query doesn't work. It doesn't work at all, even the query text is wrote as is wrote in the document...Example:
但是......但是......当有括号()时,查询不起作用。它根本不起作用,即使查询文本是按照文档中所写的那样写的......示例:
query 1:
查询1:
{"myField" : "Ceratonereis (Composetia) costae" } -> 1 result (ok)
query 2:
查询2:
{ "myField" : {
"$regex" : "Ceratonereis (Composetia) costae" ,
"$options" : "iu"
}} -> no results (not ok)
query 3:
查询3:
{ "scientificName" : {
"$regex" : "ceratonereis (composetia) costae" ,
"$options" : "iu"
}} -> no results (....)
So...I'm doing something wrong? I forgot some Pattern.SOME to include in the Pattern.compile()? Any solution?
那么......我做错了什么?我忘记了一些Pattern.Some包含在Pattern.compile()中?有解决方案吗
Thanks
谢谢
------ UPDATE ------
------更新------
The answer of user3561036 helped me to figure how the query must be built.
user3561036的答案帮助我弄清楚必须如何构建查询。
So, I have resolved by modifying the query building in
所以,我通过修改查询构建解决了
q.addCriteria(Criteria.where("myField")
.regex(Pattern.compile(Pattern.quote(myFieldValue), Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)));
The output query
输出查询
{ "myField" : { "$regex" : "\\Qhaliclona (rhizoniera) sarai\\E" , "$options" : "iu"}}
works.
作品。
2 个解决方案
#1
2
If using the $regex
operator with a "string" as input then you must quote literals for reserved characters such as ()
.
如果使用带有“string”作为输入的$ regex运算符,则必须引用保留字符(如())的文字。
Normally that's a single \
, but since it's in a string already you do it twice \\
:
通常这是一个\,但因为它已经在一个字符串中你已经两次\\:
{ "myField" : {
"$regex" : "Ceratonereis \\(Composetia\\) costae" ,
"$options" : "iu"
}}
#2
0
Use $strcasecmp. The aggregation framework was introduced in MongoDB 2.2. You can use the string operator "$strcasecmp" to make a case-insensitive comparison between strings. It's more recommended and easier than using regex.
使用$ strcasecmp。聚合框架是在MongoDB 2.2中引入的。您可以使用字符串运算符“$ strcasecmp”在字符串之间进行不区分大小写的比较。它比使用正则表达式更值得推荐和更容易。
#1
2
If using the $regex
operator with a "string" as input then you must quote literals for reserved characters such as ()
.
如果使用带有“string”作为输入的$ regex运算符,则必须引用保留字符(如())的文字。
Normally that's a single \
, but since it's in a string already you do it twice \\
:
通常这是一个\,但因为它已经在一个字符串中你已经两次\\:
{ "myField" : {
"$regex" : "Ceratonereis \\(Composetia\\) costae" ,
"$options" : "iu"
}}
#2
0
Use $strcasecmp. The aggregation framework was introduced in MongoDB 2.2. You can use the string operator "$strcasecmp" to make a case-insensitive comparison between strings. It's more recommended and easier than using regex.
使用$ strcasecmp。聚合框架是在MongoDB 2.2中引入的。您可以使用字符串运算符“$ strcasecmp”在字符串之间进行不区分大小写的比较。它比使用正则表达式更值得推荐和更容易。