I am trying to find an elegant way to update an existing MongoDB document with data retrieved from a web page as json. The problem is that I do not know in advance which fields will be updated, so I can not use set__field , I only have a json representation of the fields that will be updated in my MongoDB document. In addition, I use DynamicDocuments, so new fields can be set in the document. eg:.
class Study(DynamicDocument): study_accession_nr = StringField() study_name = StringField() study_type = StringField()
and json might look like:
{"_id" : "123", "study_name" : "Statistics"}
or
{"_id" : "123", "study_type" : "research", "study_name" : "Statistical analysis"}
I can do this easily from the console or using pymongo, but I donβt know how to do it using Mongoengine, unless I manually setattr (myDocInstance, nameOfField, val), which does not look so elegant for me. Thanks!
Clara source share