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.

6
  • 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 Commented Jan 27, 2015 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... Commented Jan 27, 2015 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 Commented Jan 27, 2015 at 17:33
  • Alright, so fromDate and toDate are java.util.Date objects, I'll see what I can dig up Commented Jan 27, 2015 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... Commented Jan 27, 2015 at 17:39

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

fromDate and toDate are java.util.Date objects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.