Aman
Aman

Reputation: 3261

How to parse ISO Java date format for mongoDB

Here is a sample document

 {  "userId" : "ABC,

        "timestamp" : ISODate("2014-09-03T22:07:01.261Z"),
        "body" : {
                "id" : "0082171",
                "name" : "XYZ",
                "palce":"Ind"
        },
        } 



  DBObject match = new BasicDBObject(); 

 match.put("body.id","0082171"); 

   match.put("body.name", new BasicDBObject("$exists",1));

I get count of 50999

When I add date field for matching criteria as below I am getting no value.

DBObject match = new BasicDBObject(); match.put("data.type","score.type"); match.put("body.name", new BasicDBObject("$exists",1)); match.put("timestamp","2014-09-03T22:07:01.261Z");

I want my java code to find value

Upvotes: 0

Views: 2865

Answers (1)

Martin
Martin

Reputation: 5322

Just use a standard java.util.Date object in your query - the Java driver will handle conversion for you.

The problem with your code is that you try to match a String to a ISODate object and that is never going to match.

Here is how your code could look:

Date timeStamp = new Calendar().set(2014,09,03,22,07,01).getTime();
DBObject match = new BasicDBObject();
match.put("data.type","score.type");
match.put("body.name", new BasicDBObject("$exists",1));
match.put("timestamp",timeStamp);

Upvotes: 2

Related Questions