0

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?

1
  • 1
    it's doing that because you are storing it as a string! Commented May 28, 2013 at 18:31

2 Answers 2

1

It looks like your date was stored as a String and not as an ISODate. If it were stored as a real date and not a string, you'd see something like:

> db.MyCollection.find()
{ "_id" : ObjectId("51a4e4aa3004bc68e4f499e5"), "date" : ISODate("2000-09-29T23:00:00Z") }

Make sure when you're saving the "time" field that you're inserting it as a Date. If you're using the Java driver to insert the values, you need to make sure that time field is a java.util.Date when you add it to DBObject (assuming you're using the Java driver directly and not some third party library).

When you've inserted it as a date then you should be able to perform the types of queries you want, using ISODate values from the shell or Date via the Java driver.

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

2 Comments

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("time", nowDate); DBCollection collection = db.getCollection(<collection name>); DBObject dbObject = (DBObject) JSON.parse(input.toString()); collection.insert(dbObject); Can you please help me understand my mistake here.
The code in your comment is creating a new Date object (new Date()), using SimpleDateFormatter to turn it into a String (dateFormat.format(new Date()), turning it back into a Date object (dateFormat.parse(nowString)). You'll get the same result in a lot less lines of code by doing input.put("time", new Date()); But that will still be wrong. What type is input and what else is in it?
0

I think your date should be an ISODate object. currently its a string in your query. try something like below.

db.coll.find({"time" : { $gte : new ISODate("2013-05-28T20:18:00Z") }});

Also, related link

1 Comment

this is the query that is getting executed. >>> db.notifications.find({ "time" : { "$gt" : { "$date" : "2013-05-24T18:30:00.000Z"}}})

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.