Skip to content

Commit

Permalink
Merge pull request #1737 from CalgaryMichael/remove-pushall
Browse files Browse the repository at this point in the history
Removing the usage of the '$pushAll' operator
  • Loading branch information
erdenezul authored Apr 17, 2018
2 parents 2d8d2e7 + 72c4444 commit 2121387
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 8 additions & 2 deletions mongoengine/queryset/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def update(_doc_cls=None, **update):
value = {key: value}
elif op == 'addToSet' and isinstance(value, list):
value = {key: {'$each': value}}
elif op == 'push':
elif op in ('push', 'pushAll'):
if parts[-1].isdigit():
key = parts[0]
position = int(parts[-1])
Expand All @@ -338,7 +338,13 @@ def update(_doc_cls=None, **update):
value = [value]
value = {key: {'$each': value, '$position': position}}
else:
value = {key: value}
if op == 'pushAll':
op = 'push' # convert to non-deprecated keyword
if not isinstance(value, (set, tuple, list)):
value = [value]
value = {key: {'$each': value}}
else:
value = {key: value}
else:
value = {key: value}
key = '$' + op
Expand Down
13 changes: 12 additions & 1 deletion tests/queryset/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,21 @@ class Doc(Document):

update = transform.update(DicDoc, pull__dictField__test=doc)
self.assertTrue(isinstance(update["$pull"]["dictField"]["test"], dict))

update = transform.update(LisDoc, pull__foo__in=['a'])
self.assertEqual(update, {'$pull': {'foo': {'$in': ['a']}}})

def test_transform_update_push(self):
"""Ensure the differences in behvaior between 'push' and 'push_all'"""
class BlogPost(Document):
tags = ListField(StringField())

update = transform.update(BlogPost, push__tags=['mongo', 'db'])
self.assertEqual(update, {'$push': {'tags': ['mongo', 'db']}})

update = transform.update(BlogPost, push_all__tags=['mongo', 'db'])
self.assertEqual(update, {'$push': {'tags': {'$each': ['mongo', 'db']}}})

def test_query_field_name(self):
"""Ensure that the correct field name is used when querying.
"""
Expand Down

0 comments on commit 2121387

Please sign in to comment.