title: MongoDB case insensitive query on text with parenthesis
body: <p>I have a very annoying problem with a case insensitive query on mongodb.</p>
  
  <p>I'm using MongoTemplate in a web application and I need to execute case insensitive queries on a collection.</p>
  
  <p>with this code </p>
  
  <pre><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);
  </code></pre>
  
  <p>I create the following query</p>
  
  <pre><code>{ "myField" : { "$regex" : "field value" , "$options" : "iu"}}
  </code></pre>
  
  <p>that works perfectly when I have simple text, for example:</p>
  
  <p><strong>caPITella CapitatA</strong></p>
  
  <p>but...but...when there are <em>parenthesis ()</em> the query doesn't work.
  It doesn't work at all, even the query text is wrote as is wrote in the document...Example:</p>
  
  <p>query 1:</p>
  
  <pre><code>{"myField" : "Ceratonereis (Composetia) costae" } -&gt; 1 result (ok)
  </code></pre>
  
  <p>query 2:</p>
  
  <pre><code>{ "myField" : { 
      "$regex" : "Ceratonereis (Composetia) costae" , 
     "$options" : "iu"
  }} -&gt; no results (not ok)
  </code></pre>
  
  <p>query 3:</p>
  
  <pre><code>{ "scientificName" : { 
      "$regex" : "ceratonereis (composetia) costae" ,
      "$options" : "iu"
  }}  -&gt; no results (....)
  </code></pre>
  
  <p>So...I'm doing something wrong? I forgot some  Pattern.SOME to include in the Pattern.compile()? Any solution?</p>
  
  <p>Thanks</p>
  
  <p><strong>------ UPDATE ------</strong></p>
  
  <p>The answer of user3561036 helped me to figure how the query must be built.</p>
  
  <p>So, I have resolved by modifying the query building in</p>
  
  <pre><code>q.addCriteria(Criteria.where("myField")
  .regex(Pattern.compile(Pattern.quote(myFieldValue), Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE))); 
  </code></pre>
  
  <p>The output query</p>
  
  <pre><code>{ "myField" : { "$regex" : "\\Qhaliclona (rhizoniera) sarai\\E" , "$options" : "iu"}}
  </code></pre>
  
  <p>works.</p>