Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions mongoengine/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,20 +467,14 @@ 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)
raw_object = wc_collection.find_one_and_replace(select_dict, doc)
if raw_object:
return doc["_id"]

object_id = wc_collection.insert_one(doc).inserted_id

return object_id
wc_collection.find_one_and_replace(select_dict, doc, upsert=True)
return doc["_id"]

def _get_update_doc(self):
"""Return a dict containing all the $set and $unset operations
Expand Down