regex - MongoDB case insensitive query on text with parenthesis -


i have annoying problem case insensitive query on mongodb.

i'm using mongotemplate in web application , need execute case insensitive queries on collection.

with 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 following query

{ "myfield" : { "$regex" : "field value" , "$options" : "iu"}} 

that works when have simple text, example:

capitella capitata

but...but...when there parenthesis () query doesn't work. doesn't work @ all, query text wrote wrote in 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 wrong? forgot pattern.some include in pattern.compile()? solution?

thanks

------ update ------

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

so, have resolved modifying 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.

if using $regex operator "string" input must quote literals reserved characters such ().

normally that's single \, since it's in string twice \\:

{ "myfield" : {      "$regex" : "ceratonereis \\(composetia\\) costae" ,      "$options" : "iu" }} 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -