title: Java & MongoDB query for date range
body: <p>Before I start, I have already searched around for an answer to this issue and the best answer I could come up with is <a href="https://stackoverflow.com/questions/6840540/java-mongodb-query-by-date">this question</a></p>
  
  <p>I have one difference though. I have a table that maintains a history of many documents. Therefore I need to query on an ID as well as the date range. Here is what my query currently looks like in Java</p>
  
  <pre><code>BasicDBObject searchQuery = new BasicDBObject();
  
  searchQuery.put("id", id);
  searchQuery.put("dateModified", BasicDBObjectBuilder.start("$gte", fromDate).add("$lte", toDate).get());
  
  DBCursor cursor = table.find(searchQuery);
  </code></pre>
  
  <p>This returns no results. The MongoQuery that is generated by this block of code looks like this:</p>
  
  <pre><code>db.history.find({ "id" : 12345 , "dateModified" : { "$gte" : { "$date" : "2015-01-19T00:00:00.000Z"} , "$lte" : { "$date" : "2015-01-25T00:00:00.000Z"}}});
  </code></pre>
  
  <p>When I manually type this into MongoDB command line, this also returns no results. I currently have one record in the database for testing purposes that looks like this:</p>
  
  <pre><code>{
      "id" : NumberLong(12345),
      "dateModified" : ISODate("2015-01-21T19:42:28.044Z")
  }
  </code></pre>
  
  <p>This object should clearly match the query, yet nothing is returning, any ideas?</p>
  
  <p><strong>EDIT</strong>: So it turns out that the string generated by the query object doesn't match the ISODate object in the database. I'd like to clarify that fromDate and toDate are both java.util.Date objects. I'm still not sure how to solve this though.</p>