Background: (using Eve and Mongo)
I'm working in Python using the Eve REST provider library connecting and to a mongoDB to expose a number of REST endpoints from the database. I've had good luck using Eve so far, but I've run into a problem that might be a bit beyond what Eve can do natively.
My problem is that my mongoDb document format has a field (called "slots"), whose value is a list/array of dictionaries/embedded-documents.
So the mongoDB document structure is:
{
blah1: data1,
blah2: data2,
...
slots: [
{thing1:data1, thing2:data2},
{thingX:dataX, thingY:dataY}
]
}
I need to add new records (I.E. add pre-populated dictionaries) to the 'slots' list.
If I imagine doing the insert directly via pymongo it would look like:
mongo.connection = MongoClient()
mongo.db = mongo.connection['myDB']
mongo.coll = mongo.db['myCollection']
...
mongo.coll.update({'_id' : document_id},
{'$push': { "slot" : {"thing1":"data1","thingX":"dataX"} } } )
The REST action/URI combo that I would like to do this action is a POST
to '_id/slots', e.g. URI of /app/012345678901234567890123/slots
.
Problem: (inserting an element into an array in Eve)
From SO: How to add to a list type in Python Eve without replacing old values and eve project issue it appears Eve doesn't currently support operating on mongoDB embedded documents (or arrays?) unless the entire embedded document is rewritten, and rewriting the whole array is very undesirable in my case.
So, assuming its true Eve doesn't have a method to allow inserting of array elements (and given I already have numerous other endpoints working well inside of Eve)...
... I'm now looking for a way, inside of an Eve/Flask configuration with multiple working endpoints, to intercept and change Eve's mongoDB write for just this one endpoint.
I know (worst case) I can override the routing of Eve and to completely do the write by hand, but then I would have manage the _updated
and hand check & change the documents _etag
value, both things I would certainly prefer not to have to write new code for.
I've looked at Eve's Datebase event hooks but I don't see a way to modify the database commands that are executed (I can see how to change the data, but not the commands).
Anyone else already solved this problem already? If not any ideas on the most direct path to implement by hand? (hopefully reusing as much of Eve as possible because I do want to continue using Eve for all my (already working) endpoints)