[JAVA-565] JSONParser issue for ISODate Created: 08/May/12  Updated: 25/Jun/13  Resolved: 27/Aug/12

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 2.7.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: M Schaffer Assignee: Bryan Reinero
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7



 Description   

I am using the JSON.parse method to parse a string query. It works fine until I have an ISODate in the query.
I've included some sample code and the error below.
If I run the query directly on the cli it works fine.

db.runCommand({aggregate:"test", pipeline:[{$match:{date:ISODate("2012-05-01T12:30")}}]});

Code:
String queryString = "{aggregate:\"test\", pipeline:[{$match:{date:ISODate(\"2012-05-01T12:30\")}}]}";
BasicDBObject query = (BasicDBObject) JSON.parse(queryString);

Error:
com.mongodb.util.JSONParseException:
{aggregate:"test", pipeline:[{$match:{date:ISODate("2012-05-01T12:30")}}]} ^
at com.mongodb.util.JSONParser.parse(JSON.java:381)
at com.mongodb.util.JSONParser.parseObject(JSON.java:414)
at com.mongodb.util.JSONParser.parse(JSON.java:378)
at com.mongodb.util.JSONParser.parseObject(JSON.java:414)
at com.mongodb.util.JSONParser.parse(JSON.java:378)
at com.mongodb.util.JSONParser.parseArray(JSON.java:718)
at com.mongodb.util.JSONParser.parse(JSON.java:374)
at com.mongodb.util.JSONParser.parseObject(JSON.java:414)
at com.mongodb.util.JSONParser.parse(JSON.java:378)
at com.mongodb.util.JSONParser.parse(JSON.java:328)
at com.mongodb.util.JSON.parse(JSON.java:287)
at com.mongodb.util.JSON.parse(JSON.java:272)



 Comments   
Comment by Bernd Zuther [ 31/Aug/12 ]

I had the same problem and i could not understand the anwser of Bryan. Then i experience a little bit, with the JSON class.

DBObject object = new BasicDBObject();
object.put("date", new Date());
String serialize = JSON.serialize(object);

serialize is: { "date" : { "$date" : "2012-08-31T17:30:01.882Z"}}

so this command should work.
db.runCommand({aggregate:"test", pipeline:[{$match:{ "date" :

{ "$date" : "2012-08-31T17:30:01.882Z"}

}}]});

PLEASE BE CAREFUL
:

{date:ISODate("2012-05-01T12:30")}

must be replaced { "date" :

{ "$date" : "2012-08-31T17:30:01.882Z"}

}}

When using the mongodb hadoop adapter it is also imported to convert dates in this way:

mongo.input.query={orderDate : {$gte : {$date : "2012-08-30T00:00:00.000Z"}, $lt: {$date : "2012-08-30T00:00:00.000Z"}}}

Comment by Bryan Reinero [ 12/Jun/12 ]

The parser accepts these two date formats: seconds -> "yyyy-MM-dd'T'HH:mm:ss'Z'" or
seconds.milleseconds -> "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

Just add seconds to your $date value and the aggregation command should work. Here's the JSON doc that I had success with:

{ "aggregate" : "test", pipeline : [ {$match : { date :

{ $date : "2012-05-01T12:30:00Z" }

} } ] }

Generated at Thu Feb 08 08:52:35 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.