using pull_all and push_all with python/mongoengine
I have this object in Mongo:
mystuff = ListField(ReferenceField(Asset, dbref=True))
I have a Python method that is supposed to update the Mongo object, prepending its mystuff Listfield value with a given value. Because Mongoengine doesn't yet have a way to insert an object into a certain point in a list (and has made it a low priority to add this function), I've tried to:
- save the contents of the current list to a temporary variable (oldlist)
- update the DB entry, emptying the mystuff list using the "pull_all" modifier (which is part of mongoengine)
- update the DB entry again, pushing the newly added item to the mystuff list using update(push)
- update the DB entry once again, using the "push_all" modifier and the oldlist variable to push the old stuff back onto the mystuff list.
It seems that "pull_all" requires some kind of modifier, but I'll be danged if I can figure out what it wants.
Anybody got any ideas? Of course the ideal situation would be to add an "insert_at" modifier to update(), but that's out of my hands. Life on the edge, etc.
The pull_all takes a list of elements you want to pull out of the list. In you're case I believe this will be oldlist.
As you noted the tools for updating the document in place are limited.