suppose we have the following document:
{ '_id': 1, 'Name': 'Document1', 'Data': { 'Color': 'Green', 'Size': 5 } }
And we want to update the document using Pymongo using a dict as source. For instance, let's suppose that this is the dict we want to use to update the document:
dict1 = { 'Name': 'Document1', 'Data': { 'Size': 5 } }
If we know for sure which are the fields contained in dict1
we could use:
collection.update_one( { 'Name': dict1['Name'] }, { '$set': { 'Data.Size': dict1['Data']['Size'] } })
In the general case in which we don't know what is inside the ['Data']
field of dict1
, the following approach:
collection.update_one( { 'Name': dict1['Name'] }, { '$set': { 'Data': dict1['Data'] } })
Will potentially delete any fields which were present in the document but are not in dict1 (in our example, 'Color'
).
Is it possible to tell Pymongo that we want to update a field (which is containing many fields) but only inserting/updating fields in the source dict?