diff --git a/docs/changelog.rst b/docs/changelog.rst index e3d366b3..d8765d17 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -10,6 +10,7 @@ dev - Fix validation error instance in GenericEmbeddedDocumentField #1067 - Update cached fields when fields argument is given #1712 - Add a db parameter to register_connection for compatibility with connect +- Use insert_one, insert_many in Document.insert #1491 - Use new update_one, update_many on document/queryset update #1491 - Use insert_one, insert_many in Document.insert #1491 diff --git a/mongoengine/queryset/base.py b/mongoengine/queryset/base.py index 0a3f65fb..008e7f15 100644 --- a/mongoengine/queryset/base.py +++ b/mongoengine/queryset/base.py @@ -350,13 +350,15 @@ class BaseQuerySet(object): documents=docs, **signal_kwargs) raw = [doc.to_mongo() for doc in docs] - insert_func = self._collection.insert_many - if return_one: - raw = raw[0] - insert_func = self._collection.insert_one + + with set_write_concern(self._collection, write_concern) as collection: + insert_func = collection.insert_many + if return_one: + raw = raw[0] + insert_func = collection.insert_one try: - inserted_result = insert_func(raw, set_write_concern(write_concern)) + inserted_result = insert_func(raw) ids = return_one and [inserted_result.inserted_id] or inserted_result.inserted_ids except pymongo.errors.DuplicateKeyError as err: message = 'Could not save document (%s)'