I saw how this could be accomplished from the mongo shell here: MongoDB: Updating documents using data from the same document
However, I cannot figure out how to do this using the python driver. My goal is thus (in MySQL equivalent):
UPDATE coll SET field1 = field1 + field2;
So far, I found that the easiest way was to adapt the linked SO answer using the pymongo.code.Code class with db.eval(), something like:
You can optionally save the js script on the server from the mongo shell with
db.system.js.save({_id:'myfunc', value: function(){...}});and execute from python with
db.eval(Code('return myfunc()'))