Skip to content

Commit 352b233

Browse files
committed
Fix bug #1965 of $position and $push operators do not work with list in an EmbeddedDocument. Set key value to joined parts excluding the index at the end. Added test case
1 parent bdd6041 commit 352b233

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,4 @@ that much better:
247247
* Erdenezul Batmunkh (https://github.com/erdenezul)
248248
* Andy Yankovsky (https://github.com/werat)
249249
* Bastien Gérard (https://github.com/bagerard)
250+
* Trevor Hall (https://github.com/tjhall13)

mongoengine/queryset/transform.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def update(_doc_cls=None, **update):
345345
value = {key: {'$each': value}}
346346
elif op in ('push', 'pushAll'):
347347
if parts[-1].isdigit():
348-
key = parts[0]
348+
key = '.'.join(parts[0:-1])
349349
position = int(parts[-1])
350350
# $position expects an iterable. If pushing a single value,
351351
# wrap it in a list.

tests/document/instance.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,10 +842,16 @@ def test_modify_update(self):
842842

843843
@requires_mongodb_gte_26
844844
def test_modify_with_positional_push(self):
845+
class Content(EmbeddedDocument):
846+
keywords = ListField(StringField())
847+
845848
class BlogPost(Document):
846849
tags = ListField(StringField())
850+
content = EmbeddedDocumentField(Content)
851+
852+
post = BlogPost.objects.create(
853+
tags=['python'], content=Content(keywords=['ipsum']))
847854

848-
post = BlogPost.objects.create(tags=['python'])
849855
self.assertEqual(post.tags, ['python'])
850856
post.modify(push__tags__0=['code', 'mongo'])
851857
self.assertEqual(post.tags, ['code', 'mongo', 'python'])
@@ -856,6 +862,16 @@ class BlogPost(Document):
856862
['code', 'mongo', 'python']
857863
)
858864

865+
self.assertEqual(post.content.keywords, ['ipsum'])
866+
post.modify(push__content__keywords__0=['lorem'])
867+
self.assertEqual(post.content.keywords, ['lorem', 'ipsum'])
868+
869+
# Assert same order of the list items is maintained in the db
870+
self.assertEqual(
871+
BlogPost._get_collection().find_one({'_id': post.pk})['content']['keywords'],
872+
['lorem', 'ipsum']
873+
)
874+
859875
def test_save(self):
860876
"""Ensure that a document may be saved in the database."""
861877

0 commit comments

Comments
 (0)