Skip to content

Commit d88d40c

Browse files
author
erdenezul
authored
Merge pull request #1966 from tjhall13/1965-fix-position-op-with-nested-list-in-embedded-document
Fix bug #1965 of $position and $push operators do not work with nested list
2 parents bdd6041 + d3b4af1 commit d88d40c

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-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)

docs/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Development
66
===========
77
- (Fill this out as you fix issues and develop your features).
88

9+
=================
10+
Changes in 0.16.3
11+
=================
12+
- Fix $push with $position operator not working with lists in embedded document #1965
13+
914
=================
1015
Changes in 0.16.2
1116
=================

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)