I’m trying to get the data that was updated or inserted into the collection. This is for implementing version history (change stream isn’t a good fit for our use case, so we need to use custom solution).
Basically when the document is initially added, the full document is added to the version history, but any updates afterwards will only include fields that changed from the previous version.
This is the closest I’ve gotten:
# Code
for item in import_data:
result = collection.update_one({"name": item["name"], "serial_number": item["serial_number"]}, {"$set": item}, upsert=True)
print(result.raw_result)
# Printed when document was inserted:
{
'n': 1,
'upserted': ObjectId('6571a3d450ed09ae8776e2eb'),
'nModified': 0,
'ok': 1.0,
'updatedExisting': False
}
# Printed when document was updated:
{
'n': 1,
'nModified': 1,
'ok': 1.0,
'updatedExisting': True
}
Of course this is something that I can do manually in python, but if there is already a way to get the data from the update/insert query without having to do another query, it would be better. It would be simpler and probably faster too.