I am using com.mongodb.util.JSON.parse to parse a JSON file to DBObject. How do I specify dates, refs, and objects IDs in the JSON file?
1 Answer
Dates : { myDate: {$date: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" } } // Date in the mentioned ISODate string format.
Refs : { myRef : { $ref : <collname>, $id : <idvalue>[, $db : <dbname>] } } // collname is collection name, idvalue is the _id of the referred document and optionally dbname is the database the document is in.
ObjectIds : { _id : {$oid: "4e942f36de3eda51d5a7436c"} }
4 Comments
jeffreyveon
Can you please explain what you mean by a parseable string?
Remon van Vliet
Any particular reason why you unaccepted. The information above is accurate.
jeffreyveon
Sorry, I meant to follow up. I have accepted the answer, but I am facing an issue. The Java library expects the data to be in the ISO date format (as a string), and not as a long as given above. When I use the long format, I get a JSON parse exception.
Remon van Vliet
@jeffreyveon. Ah, yes, my apologies. The JSON.parse method expects the date as a string in the "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" format. I'll edit my answer