I'm building a REST api with python, tornadoweb and MongoDB. I'm looking for a simple way to handle client-server document exchanges with the lowest operations and the simplest API as possible. I'm facing a problem to serialize and deserialize MongoDB's ObjectId to JSON to send/receive data to and from the client.
I know there is two ways to fix this:
- Configuring MongoDB to use string _id fields instead of ObjectId via SONManipulators (Configure pymongo to use string _id instead of ObjectId)
- Using json_utils module to convert ObjectId to the json string
{"$oid": "..."}
(Unable to deserialize PyMongo ObjectId from JSON)
None of these solutions seems to be acceptable because:
- MongoDB uses ObjectIds instead of strings for a reason (cf: MongoDb: Benefit of using ObjectID vs a string containing an Id?)
If the server is sending the id of a document with the syntax
{"$oid": "..."}
, when the client sends back this id without any modification, tornado'sget_argument
function is not able to handle properly the data it receives:> arguments: {'_id[$oid]': ['54f9c7ab834bac1b76846655'], ...} > POST [...] (127.0.0.1): Missing argument _id
Any idea about how to handle this issue gracefully? By gracefully I mean that I would like to handle the id in a way that is the most similar to what I would do if the id was a simple string.
Thanks a lot for your help. I did some researches, I'm surprised I couldn't find an answer to this question as both MondoDB and tornado are quite often used together. Maybe I'm missing a keyword to get the solution.
{obj: {field: 'value'}}
as{"obj[field]": "value"}
– Romain G