title: How do I prevent Java's Mongo driver from escaping quotes in queries
body: <h2>The case</h2>
  
  <p>So I'm trying to execute a query to a Mongo database using Java. In shell, the query I'm talking about looks like this:</p>
  
  <pre><code>db.myCollection.find({ "array" : { "$in" : [ "foo" , "bar" ]}});
  </code></pre>
  
  <p>The query is put together in Java like so:</p>
  
  <pre><code>DBCursor cursor = myCollection.find(new BasicDBObject("array", new BasicDBObject("$in", items)));
  </code></pre>
  
  <p>Here, variable <strong>items</strong> is defined as a String[], which is passed from somewhere else. It can hold any value, but in this example it contains the following elements:</p>
  
  <pre><code>"foo", "bar";
  </code></pre>
  
  <h2>The problem</h2>
  
  <p>This is where the problem starts. Calling</p>
  
  <pre><code>cursor.getQuery()
  </code></pre>
  
  <p>returns</p>
  
  <pre><code>{ "array" : { "$in" : [ "\"foo\"" , "\"bar\"" ]}}
  </code></pre>
  
  <p>which won't give back any results. As you can see, the Mongo driver for Java added quotes and escaped them.</p>
  
  <h2>I tried</h2>
  
  <ol>
  <li><p>Copying the array, and making sure everything is trimmed off. Then pass it to the query.</p>
  
  <pre><code>String[] test = new String[items.length];
  for(int i = 0; i &lt; items.length; i++){
      test[i] = items[i].trim();
  }
  </code></pre>
  
  <p>Didn't work.</p></li>
  <li><p>Now here's some real magic for y'all! I tried initializing the string just before the query like this:</p>
  
  <pre><code>String[] items = new String[]{ "foo", "bar" };
  </code></pre>
  
  <p>When I initialize it like that, Mongo doesn't escape the quotes and the query works.</p></li>
  </ol>
  
  <hr>
  
  <p>So I'd like to know what kind of trickery this is. And how I could solve this.</p>
  
  <p>Thank you all for reading!</p>