How to check if the update was successful in Pymongo? If the record was updated with the same value it should be considered a successful update. ie, Even if the modified count is zero it is considered a successful update. How to do this.
result = _db.testcollection.update_one({"_id": col_id}, {"$set": obj})
return True if result.modified_count > 0 else False
This works well if the value is changed. But I want to return true even when technically the record was not changed because the record is same what we are trying to update.
How to do this?
The below solution worked for me.Check matched_count instead of modified_count. If there were a matched records and no exception it means that it is successful.
try:
result = _db.testcollection.update_one({"_id": col_id}, {"$set": obj})
return result.matched_count > 0
except:pymongo.errors.PyMongoError as e:
return False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With