Skip to content
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.

Date/time format when parsing document from mongo extended json #193

Open
landler opened this issue Oct 1, 2014 · 0 comments
Open

Date/time format when parsing document from mongo extended json #193

landler opened this issue Oct 1, 2014 · 0 comments

Comments

@landler
Copy link

landler commented Oct 1, 2014

Currently there doesn't seem to be a way to properly parse mongodb extended json datetimes into a document with from_json. As an example consider this extended json document:

{
   "id" : {"$oid": "507f1f77bcf86cd799439011"},
   "time" : {"$date" : 18000000}
}

The id is properly parsed with a structure as follows:

{
    "id" : bson.objectid.ObjectId,
    "time": datetime.datetime
}

However, parsing the date fails since mongokit/document.py:586 just calls fromtimestamp with the whole {"$date".. dict as an argument. This could be solved by using a structure where "time": {"$date": datetime.datetime}, but this doesn't work since keys starting with $ are not allowed.

It seems to me it would be optimal if mongokit would be able to parse the standard mongodb extended json out of the box - for a variety of reasons. Thus it would probably make sense to either replace line 586 with doc[key] = fromtimestamp(doc[key]["$date"]) , (just like is done with $oid for objectid fields or $db and $ref) or if there are fears of breaking backwards compatibility by checking the type of doc[key] and whether there is a $date key if it is a dict. I can fork and make a pull request if you agree with my reasoning.

On a related note, since mongodb 2.6 the extended json format also uses ISO datetime strings instead of milliseconds since epoch - so the current parser fails there as well.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant