Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass upsert=True to find_one_and_replace() on Document.save() method. #2581

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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