(I know I just asked another question about this, but the answers lead me to ask this question, I thought it would be appropriate to create another)
I have an object in MongoDB that looks like this:
{
"id" : NumberLong(12345),
"dateModified" : ISODate("2015-01-21T19:43:17.440Z")
}
The query I need to create to retrieve this should look like this (the date in the object falls between these two dates):
db.history.find({"dateModified" : { "$gte" : ISODate("2015-01-19T00:00:00.000Z") , "$lte" : ISODate("2015-01-25T00:00:00.000Z")}});
Unfortunately, I'm using Java and things are coming out funny. Here is my Java query:
java.util.Date fromDate;
java.util.Date toDate;
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("dateModified", BasicDBObjectBuilder.start("$gte", fromDate).add("$lte", toDate).get());
Pretty much everywhere I go on the internet suggests that I use the above Java code to perform this query, but the text output for this query is:
{"dateModified" : { "$gte" : { "$date" : "2015-01-19T00:00:00.000Z"} , "$lte" : { "$date" : "2015-01-25T00:00:00.000Z"}}
This query returns no results because it isn't querying in the ISODate format. EDIT: What I mean here is that if I use the above query in the mongo shell I get no results, but if I use the first query I posted, I do get results.
My question is why doesn't this automatically convert to ISODate like the everyone says it should? Or, what can I do to make sure this query converts to the ISODate format?