[Answered ]-Pymongo Not inserting full document on update_one

1👍

To "upsert" a full document and additional fields using python, you can use MongoDB’s "$setOnInsert" with a python merged dictionary.

From the python library docs, here’s how you merge dictionaries. (It’s similar to MongoDB’s "$mergeObjects".)

d | other

Create a new dictionary with the merged keys and values of d and other,
which must both be dictionaries. The values of other take priority
when d and other share keys.

So, to insert the full document, using your python code, it just needs a minor addition – merge document with your other object.

...
update={'$setOnInsert': document | {'insert_date': now}, '$set': {'update_date': now, }}
...

Leave a comment