Mongodb find contains, startswith, endswith, and exact string with java driver
<p>I'm trying to do a find on my database for strings. These strings contain a wildcard for contains, startswith, and endswith; otherwise I just search for the exact value. I've looked at many examples of doing search with java but they all seem to be outdated with the current java driver and use BasicDBobjects which I cant get to work. I believe the code below will work 
I just need to know how I need to change some of the strings to search the database:</p>

<p>Let's say my database looks like this</p>

<pre><code>{State:Ohio}
{State:Iowa}
{State:Texas}
</code></pre>

<p>containsString: returns any document that contains it. ie "io" would return Ohio and Iowa documents</p>

<p>beginString: returns any documents that start with it. ie "io" would retun Iowa document</p>

<p>endString: returns  any documents that end with it. ie "io" would return Ohio document</p>

<p>else it will look for the exact value. ie "texas" would return Texas document</p>

<pre><code>MongoDatabase db = mongoClient.getDatabase("database1");
MongoCollection collection = db.getCollection("collection1");
BasicDBList list = new BasicDBList();
for(int i=0; i &lt; data.length; i++){
     String[] queries = data[i].split(":", 2);
     String column = queries[0];
     String query = queries[1];
     if (query.startsWith("*") &amp;&amp; query.endsWith("*")) {
        String containsString = query.replace("*", "");
        MongoCursor&lt;Document&gt; cursor = collection.find(new Document(column, containsString)).iterator();
       while (cursor.hasNext()) {
          list.add(cursor.next());
       }
     } else if (query.startsWith("*")) {
       String beginString = query.replace("*", "");
       MongoCursor&lt;Document&gt; cursor = collecton.find(new Document(column, beginString)).iterator();
       while (cursor.hasNext()) {
          list.add(cursor.next());
       }
     } else if (query.endsWith("*")) {
       String endString = query.replace("*", "");
       MongoCursor&lt;Document&gt; cursor = collection.find(new Document(column, endString)).iterator();
       while (cursor.hasNext()) {
          list.add(cursor.next());
       }
     } else {
       MongoCursor&lt;Document&gt; cursor = collection.find(new Document(column, query)).iterator();
       while (cursor.hasNext()) {
          list.add(cursor.next());
       }
     }
}
</code></pre>