Skip to content

Commit 94e4c90

Browse files
idocyabrabagerard
authored andcommitted
add array filter to modify
1 parent 6f7f7b7 commit 94e4c90

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

mongoengine/queryset/base.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ def upsert_one(self, write_concern=None, read_concern=None, **update):
634634
document = self._document.objects.with_id(atomic_update.upserted_id)
635635
return document
636636

637-
def update_one(self, upsert=False, write_concern=None, full_result=False, **update):
637+
def update_one(self, upsert=False, write_concern=None, full_result=False, array_filters=None, **update):
638638
"""Perform an atomic update on the fields of the first document
639639
matched by the query.
640640
@@ -647,6 +647,7 @@ def update_one(self, upsert=False, write_concern=None, full_result=False, **upda
647647
will force an fsync on the primary server.
648648
:param full_result: Return the associated ``pymongo.UpdateResult`` rather than just the number
649649
updated items
650+
:param array_filters: A list of filters specifying which array elements an update should apply.
650651
:param update: Django-style update keyword arguments
651652
full_result
652653
:returns the number of updated documents (unless ``full_result`` is True)
@@ -656,11 +657,14 @@ def update_one(self, upsert=False, write_concern=None, full_result=False, **upda
656657
multi=False,
657658
write_concern=write_concern,
658659
full_result=full_result,
660+
array_filters=array_filters,
659661
**update,
660662
)
661663

662664
def modify(
663-
self, upsert=False, full_response=False, remove=False, new=False, **update
665+
self, upsert=False, full_response=False, remove=False, new=False,
666+
array_filters=None,
667+
**update
664668
):
665669
"""Update and return the updated document.
666670
@@ -680,6 +684,7 @@ def modify(
680684
:param remove: remove rather than updating (default ``False``)
681685
:param new: return updated rather than original document
682686
(default ``False``)
687+
:param array_filters: A list of filters specifying which array elements an update should apply.
683688
:param update: Django-style update keyword arguments
684689
"""
685690

@@ -717,6 +722,7 @@ def modify(
717722
upsert=upsert,
718723
sort=sort,
719724
return_document=return_doc,
725+
array_filters=array_filters,
720726
**self._cursor_args,
721727
)
722728
except pymongo.errors.DuplicateKeyError as err:

tests/queryset/test_queryset.py

+16
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,22 @@ class Blog(Document):
615615

616616
assert testc_blogs.count() == 1
617617

618+
# modify
619+
Blog.drop_collection()
620+
621+
# update one
622+
Blog.objects.create(tags=["test1", "test2", "test3"])
623+
624+
new_blog = Blog.objects().modify(
625+
__raw__={"$set": {"tags.$[element]": "test11111"}},
626+
array_filters=[{"element": {"$eq": "test2"}}],
627+
new=True,
628+
)
629+
testc_blogs = Blog.objects(tags="test11111")
630+
assert new_blog == testc_blogs.first()
631+
632+
assert testc_blogs.count() == 1
633+
618634
Blog.drop_collection()
619635

620636
# update one inner list

0 commit comments

Comments
 (0)