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

Add kwargs into insert method #2169 #2170

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Development
- ``ListField`` now accepts an optional ``max_length`` parameter. #2110
- The codebase is now formatted using ``black``. #2109
- In bulk write insert, the detailed error message would raise in exception.
- ``Doc.objects.insert`` accepts kwargs to pass ``ordered``, ``bypass_document_validation``, etc. #2169

Changes in 0.18.2
=================
Expand Down
9 changes: 7 additions & 2 deletions mongoengine/queryset/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ def first(self):
return result

def insert(
self, doc_or_docs, load_bulk=True, write_concern=None, signal_kwargs=None
self, doc_or_docs, load_bulk=True, write_concern=None, signal_kwargs=None,
**kwargs
erdenezul marked this conversation as resolved.
Show resolved Hide resolved
):
"""bulk insert documents

Expand Down Expand Up @@ -323,6 +324,10 @@ def insert(
return_one = True
docs = [docs]

if return_one and 'ordered' in kwargs:
# insert_one does not accept `ordered` argument
kwargs.pop('ordered')

for doc in docs:
if not isinstance(doc, self._document):
msg = "Some documents inserted aren't instances of %s" % str(
Expand All @@ -345,7 +350,7 @@ def insert(
insert_func = collection.insert_one

try:
inserted_result = insert_func(raw)
inserted_result = insert_func(raw, **kwargs)
ids = (
[inserted_result.inserted_id]
if return_one
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a potential bug related to the following code (that is a bit below in this method):

        # Apply inserted_ids to documents
        for doc, doc_id in zip(docs, ids):
            doc.pk = doc_id

In case you provide ordered=False and it raises an error (e.g if you try to insert 2 times a document with same pk), that piece of code won't be reached, leaving the mongoengine documents without id's. Instead of relying on zip to get the corresponding id, we could rely on the raw dict that actually gets updated with the id by pymongo (see this post). Now since it will raise an error anyway, perhaps it could be acceptable to not set the id on the Documents instances. What do you think?

Copy link
Collaborator

@bagerard bagerard Sep 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw in case of successful unordered insert, I'm also unsure if we could rely on zip , as I don't know if we can rely on the order of inserted_result.inserted_ids

Expand Down
6 changes: 6 additions & 0 deletions tests/queryset/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,12 @@ class Blog(Document):
Blog.objects.insert(Blog(title=blog2.title))

self.assertEqual(Blog.objects.count(), 2)
# pass ordered=False
erdenezul marked this conversation as resolved.
Show resolved Hide resolved
Blog.drop_collection()
Blog.objects.insert([
Blog(title="foo", posts=[post1, post2]),
Blog(title="bar", posts=[post2, post3])
], ordered=False)

def test_bulk_insert_different_class_fails(self):
class Blog(Document):
Expand Down