Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Changelog
Development
===========
- (Fill this out as you fix issues and develop your features).
=======
- Remove deprecated `save()` method and used `insert_one()` #1899

=================
Changes in 0.16.0
=================
- Various improvements to the doc
Expand Down
21 changes: 15 additions & 6 deletions mongoengine/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
TopLevelDocumentMetaclass, get_document)
from mongoengine.common import _import_class
from mongoengine.connection import DEFAULT_CONNECTION_NAME, get_db
from mongoengine.context_managers import switch_collection, switch_db
from mongoengine.context_managers import (set_write_concern,
switch_collection,
switch_db)
from mongoengine.errors import (InvalidDocumentError, InvalidQueryError,
SaveConditionError)
from mongoengine.python_support import IS_PYMONGO_3
Expand Down Expand Up @@ -429,11 +431,18 @@ def _save_create(self, doc, force_insert, write_concern):
Helper method, should only be used inside save().
"""
collection = self._get_collection()

if force_insert:
return collection.insert(doc, **write_concern)

object_id = collection.save(doc, **write_concern)
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:
raw_object = wc_collection.find_one_and_replace(
{'_id': doc['_id']}, doc)
if raw_object:
return doc['_id']

object_id = wc_collection.insert_one(doc).inserted_id

# In PyMongo 3.0, the save() call calls internally the _update() call
# but they forget to return the _id value passed back, therefore getting it back here
Expand Down