0

Before I start, I have already searched around for an answer to this issue and the best answer I could come up with is this question

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

BasicDBObject searchQuery = new BasicDBObject();

searchQuery.put("id", id);
searchQuery.put("dateModified", BasicDBObjectBuilder.start("$gte", fromDate).add("$lte", toDate).get());

DBCursor cursor = table.find(searchQuery);

This returns no results. The MongoQuery that is generated by this block of code looks like this:

db.history.find({ "id" : 12345 , "dateModified" : { "$gte" : { "$date" : "2015-01-19T00:00:00.000Z"} , "$lte" : { "$date" : "2015-01-25T00:00:00.000Z"}}});

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:

{
    "id" : NumberLong(12345),
    "dateModified" : ISODate("2015-01-21T19:42:28.044Z")
}

This object should clearly match the query, yet nothing is returning, any ideas?

EDIT: 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.

Community
  • 1
  • 1
the_camino
  • 356
  • 5
  • 18
  • Is it possible because you're passing the date as a string like `"2015-01-19T00:00:00.000Z"` that it isn't being interpreted as an `ISODate`? Try manually modifying the query with the `ISODate` syntax and running from the Mongo Shell and see if that's it – Alex Jan 27 '15 at 17:26
  • That fixed it.... So I either need to convert my query into an ISODate or change the date type in the database... – the_camino Jan 27 '15 at 17:32
  • I think you want it to be an `ISODate` in MongoDB so it can be indexed and searched as a date. You just have to fix how your query is being generated, that's all – Alex Jan 27 '15 at 17:33
  • Alright, so fromDate and toDate are java.util.Date objects, I'll see what I can dig up – the_camino Jan 27 '15 at 17:35
  • I'm just confused that when I used java.util.Date to insert into MongoDB it was automatically converted to the ISODate format. But when I use the same object to query, it is converted into that weird string... – the_camino Jan 27 '15 at 17:39
  • According to http://docs.mongodb.org/ecosystem/drivers/java-types/, you should only need to use `java.util.Date` Object's, as far as I know.. – Alex Jan 27 '15 at 20:30

2 Answers2

3

I figured out the issue. I don't understand the cause, but the issue is with the BasicDBObjectBuilder not using the Date object correctly. I switched to QueryBuilder and built the exact same query and it returned results.

the_camino
  • 356
  • 5
  • 18
2

fromDate must be of the type Date not the String representation. An ISODate in the MongoDB storage Engine is not equal to the String representation of the same date and so they do not match.

Simulant
  • 19,190
  • 8
  • 63
  • 98