Permalink
Browse files

Updates can now take raw queries

  • Loading branch information...
1 parent 3300f40 commit 6a229cfbc5a26b87a25ca8d842f8f9bf76f3b3dd @rozza rozza committed Feb 24, 2012
Showing with 26 additions and 4 deletions.
  1. +3 −0 mongoengine/queryset.py
  2. +23 −4 tests/queryset.py
View
@@ -1279,6 +1279,9 @@ def _transform_update(cls, _doc_cls=None, **update):
mongo_update = {}
for key, value in update.items():
+ if key == "__raw__":
+ mongo_update.update(value)
+ continue
parts = key.split('__')
# Check for an operator and transform to mongo-style if there is
op = None
View
@@ -1420,20 +1420,39 @@ class BlogPost(Document):
BlogPost.drop_collection()
- def test_update_pull(self):
+ def test_update_push_and_pull(self):
"""Ensure that the 'pull' update operation works correctly.
"""
class BlogPost(Document):
slug = StringField()
tags = ListField(StringField())
- post = BlogPost(slug="test", tags=['code', 'mongodb', 'code'])
+ BlogPost.drop_collection()
+
+ post = BlogPost(slug="test")
post.save()
+ BlogPost.objects.filter(id=post.id).update(push__tags="code")
+ post.reload()
+ self.assertEqual(post.tags, ["code"])
+
+ BlogPost.objects.filter(id=post.id).update(push_all__tags=["mongodb", "code"])
+ post.reload()
+ self.assertEqual(post.tags, ["code", "mongodb", "code"])
+
BlogPost.objects(slug="test").update(pull__tags="code")
post.reload()
- self.assertTrue('code' not in post.tags)
- self.assertEqual(len(post.tags), 1)
+ self.assertEqual(post.tags, ["mongodb"])
+
+
+ BlogPost.objects(slug="test").update(pull_all__tags=["mongodb", "code"])
+ post.reload()
+ self.assertEqual(post.tags, [])
+
+ BlogPost.objects(slug="test").update(__raw__={"$addToSet": {"tags": {"$each": ["code", "mongodb", "code"]}}})
+ post.reload()
+ self.assertEqual(post.tags, ["code", "mongodb"])
+
def test_update_one_pop_generic_reference(self):

0 comments on commit 6a229cf

Please sign in to comment.