From 85e2325aa67e7f003db0d170b53e805a971dc9d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Puente-Sarr=C3=ADn?= Date: Thu, 28 Oct 2021 03:38:25 -0500 Subject: [PATCH 1/2] Pass upsert to find_one_and_replace() on save. --- mongoengine/document.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/mongoengine/document.py b/mongoengine/document.py index 72e2818a5..80c63c111 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -474,13 +474,11 @@ def _save_create(self, doc, force_insert, write_concern): if "_id" in doc: select_dict = {"_id": doc["_id"]} select_dict = self._integrate_shard_key(doc, select_dict) - raw_object = wc_collection.find_one_and_replace(select_dict, doc) - if raw_object: - return doc["_id"] + wc_collection.find_one_and_replace(select_dict, doc, upsert=True) + return doc["_id"] - object_id = wc_collection.insert_one(doc).inserted_id - - return object_id + insert_one_result = wc_collection.insert_one(doc) + return insert_one_result.inserted_id def _get_update_doc(self): """Return a dict containing all the $set and $unset operations From b80c78d61fcb1b4dbfa040dc43861f9254ddf18a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Puente-Sarr=C3=ADn?= Date: Thu, 28 Oct 2021 22:53:36 -0500 Subject: [PATCH 2/2] Improving readability. --- mongoengine/document.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/mongoengine/document.py b/mongoengine/document.py index 80c63c111..5d4387455 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -467,19 +467,15 @@ def _save_create(self, doc, force_insert, write_concern): """ collection = self._get_collection() with set_write_concern(collection, write_concern) as wc_collection: - if force_insert: - return wc_collection.insert_one(doc).inserted_id - # insert_one will provoke UniqueError alongside save does not - # therefore, it need to catch and call replace_one. - if "_id" in doc: + if force_insert or "_id" not in doc: + insert_one_result = wc_collection.insert_one(doc) + return insert_one_result.inserted_id + else: select_dict = {"_id": doc["_id"]} select_dict = self._integrate_shard_key(doc, select_dict) wc_collection.find_one_and_replace(select_dict, doc, upsert=True) return doc["_id"] - insert_one_result = wc_collection.insert_one(doc) - return insert_one_result.inserted_id - def _get_update_doc(self): """Return a dict containing all the $set and $unset operations that should be sent to MongoDB based on the changes made to this