Skip to content

Commit

Permalink
Merge pull request #1966 from tjhall13/1965-fix-position-op-with-nest…
Browse files Browse the repository at this point in the history
…ed-list-in-embedded-document

Fix bug #1965 of $position and $push operators do not work with nested list
  • Loading branch information
erdenezul authored Dec 6, 2018
2 parents bdd6041 + d3b4af1 commit d88d40c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,4 @@ that much better:
* Erdenezul Batmunkh (https://github.com/erdenezul)
* Andy Yankovsky (https://github.com/werat)
* Bastien Gérard (https://github.com/bagerard)
* Trevor Hall (https://github.com/tjhall13)
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Development
===========
- (Fill this out as you fix issues and develop your features).

=================
Changes in 0.16.3
=================
- Fix $push with $position operator not working with lists in embedded document #1965

=================
Changes in 0.16.2
=================
Expand Down
2 changes: 1 addition & 1 deletion mongoengine/queryset/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def update(_doc_cls=None, **update):
value = {key: {'$each': value}}
elif op in ('push', 'pushAll'):
if parts[-1].isdigit():
key = parts[0]
key = '.'.join(parts[0:-1])
position = int(parts[-1])
# $position expects an iterable. If pushing a single value,
# wrap it in a list.
Expand Down
18 changes: 17 additions & 1 deletion tests/document/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,10 +842,16 @@ def test_modify_update(self):

@requires_mongodb_gte_26
def test_modify_with_positional_push(self):
class Content(EmbeddedDocument):
keywords = ListField(StringField())

class BlogPost(Document):
tags = ListField(StringField())
content = EmbeddedDocumentField(Content)

post = BlogPost.objects.create(
tags=['python'], content=Content(keywords=['ipsum']))

post = BlogPost.objects.create(tags=['python'])
self.assertEqual(post.tags, ['python'])
post.modify(push__tags__0=['code', 'mongo'])
self.assertEqual(post.tags, ['code', 'mongo', 'python'])
Expand All @@ -856,6 +862,16 @@ class BlogPost(Document):
['code', 'mongo', 'python']
)

self.assertEqual(post.content.keywords, ['ipsum'])
post.modify(push__content__keywords__0=['lorem'])
self.assertEqual(post.content.keywords, ['lorem', 'ipsum'])

# Assert same order of the list items is maintained in the db
self.assertEqual(
BlogPost._get_collection().find_one({'_id': post.pk})['content']['keywords'],
['lorem', 'ipsum']
)

def test_save(self):
"""Ensure that a document may be saved in the database."""

Expand Down

0 comments on commit d88d40c

Please sign in to comment.