Skip to content

Commit 2121387

Browse files
author
erdenezul
authored
Merge pull request #1737 from CalgaryMichael/remove-pushall
Removing the usage of the '$pushAll' operator
2 parents 2d8d2e7 + 72c4444 commit 2121387

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

mongoengine/queryset/transform.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def update(_doc_cls=None, **update):
328328
value = {key: value}
329329
elif op == 'addToSet' and isinstance(value, list):
330330
value = {key: {'$each': value}}
331-
elif op == 'push':
331+
elif op in ('push', 'pushAll'):
332332
if parts[-1].isdigit():
333333
key = parts[0]
334334
position = int(parts[-1])
@@ -338,7 +338,13 @@ def update(_doc_cls=None, **update):
338338
value = [value]
339339
value = {key: {'$each': value, '$position': position}}
340340
else:
341-
value = {key: value}
341+
if op == 'pushAll':
342+
op = 'push' # convert to non-deprecated keyword
343+
if not isinstance(value, (set, tuple, list)):
344+
value = [value]
345+
value = {key: {'$each': value}}
346+
else:
347+
value = {key: value}
342348
else:
343349
value = {key: value}
344350
key = '$' + op

tests/queryset/transform.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,21 @@ class Doc(Document):
5454

5555
update = transform.update(DicDoc, pull__dictField__test=doc)
5656
self.assertTrue(isinstance(update["$pull"]["dictField"]["test"], dict))
57-
57+
5858
update = transform.update(LisDoc, pull__foo__in=['a'])
5959
self.assertEqual(update, {'$pull': {'foo': {'$in': ['a']}}})
6060

61+
def test_transform_update_push(self):
62+
"""Ensure the differences in behvaior between 'push' and 'push_all'"""
63+
class BlogPost(Document):
64+
tags = ListField(StringField())
65+
66+
update = transform.update(BlogPost, push__tags=['mongo', 'db'])
67+
self.assertEqual(update, {'$push': {'tags': ['mongo', 'db']}})
68+
69+
update = transform.update(BlogPost, push_all__tags=['mongo', 'db'])
70+
self.assertEqual(update, {'$push': {'tags': {'$each': ['mongo', 'db']}}})
71+
6172
def test_query_field_name(self):
6273
"""Ensure that the correct field name is used when querying.
6374
"""

0 commit comments

Comments
 (0)