I am trying to compare dates in the below collection
$db.notifications.find() { "_id" : ObjectId("51a4467be4b0142fc8b80eda"), "time" : "Tue May 28 11:24:03 IST 2013", "KEY" : "VALUE1" } { "_id" : ObjectId("51a4467be4b0142fc8b80edb"), "time" : "Tue May 28 11:24:03 IST 2013", "KEY" : "VALUE2" }
using
$db.notifications.find({ "time" : { "$gt" : "Fri May 31 00:00:00 IST 2013"}})
but what I am observing is that it is comparing treating "time" as String an not as Date and thus the second command is returning me both the entries.
$ db.notifications.find({ "time" : { "$gt" : "Fri May 31 00:00:00 IST 2013"}}) { "_id" : ObjectId("51a4467be4b0142fc8b80eda"), "time" : "Tue May 28 11:24:03 IST 2013", "KEY" : "VALUE1" } { "_id" : ObjectId("51a4467be4b0142fc8b80edb"), "time" : "Tue May 28 11:24:03 IST 2013", "KEY" : "VALUE2" }
Can you please help me with the correct query and also let me know how that correct version can be written in JAVA.
Thanks much !!
EDIT1 :
I am saving date using the below code
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
dateFormat.setLenient(false);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String nowString = dateFormat.format(new Date());
Date nowDate = dateFormat.parse(nowString);
input.put("formattedDate", nowDate);
DBCollection collection = db.getCollection(<collection name>);
DBObject dbObject = (DBObject) JSON.parse(input.toString());
collection.insert(dbObject);"
and querying using
DBObject query = QueryBuilder.start().put("formattedDate")
.greaterThan(prevDate).get();
DBCursor cursorDocJSON = collection.find(query);
I see that I am saving "time" as Date only. Why is still there an issue?