Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

regex - MongoDB case insensitive query on text with parenthesis

I have a very annoying problem with a case insensitive query on mongodb.

I'm using MongoTemplate in a web application and I need to execute case insensitive queries on a collection.

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

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:

{"myField" : "Ceratonereis (Composetia) costae" } -> 1 result (ok)

query 2:

{ "myField" : { 
    "$regex" : "Ceratonereis (Composetia) costae" , 
   "$options" : "iu"
}} -> no results (not ok)

query 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?

Thanks

------ UPDATE ------

The answer of user3561036 helped me to figure how the query must be built.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If using the $regex operator with a "string" as input then you must quote literals for reserved characters such as ().

Normally that's a single , but since it's in a string already you do it twice \:

{ "myField" : { 
    "$regex" : "Ceratonereis \(Composetia\) costae" , 
    "$options" : "iu"
}}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...