Mongo ISODate query in Java

Skynet picture Skynet · Feb 1, 2012 · Viewed 30.2k times · Source

I have a mongo query to be executed :

query = { "dateField" : { "$gte" : ISODate('2011-11-10T07:45:32.962Z')  } }

When I do a db.Collection.find(query) on the mongo shell, I am able to retrieve the results.

How could I query this using Java ? I tried constructing a String based on the Date parameter. But in the process of building the String, it eventually gets passed as "ISODate('2011-11-10T07:45:32.962Z')" instead of ISODate('2011-11-10T07:45:32.962Z') (without the surrounding quotes).

What would be the best way to construct this query using the Java API ?

Thanks !

Answer

Eve Freeman picture Eve Freeman · Feb 1, 2012

Use a regular Java Date--also I recommend the QueryBuilder:

Date d = new Date(); // or make a date out of a string...
DBObject query = QueryBuilder.start().put("dateField").greaterThanEquals(d).get();