From 99e943c365e4d14fe51979628efbd00fb1d03c91 Mon Sep 17 00:00:00 2001 From: Bruno Rocha Date: Wed, 2 Jul 2014 14:39:29 -0300 Subject: [PATCH] Updates with no operator should default to $set Fix #667 --- docs/guide/querying.rst | 7 +++++++ mongoengine/queryset/transform.py | 3 +++ tests/document/instance.py | 6 ++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/guide/querying.rst b/docs/guide/querying.rst index 34996dc6..4a30b204 100644 --- a/docs/guide/querying.rst +++ b/docs/guide/querying.rst @@ -525,6 +525,13 @@ modifier comes before the field, not after it:: >>> post.tags ['database', 'nosql'] +.. note:: + + If no modifier operator is specified the default will be `set` the both following sentences are identical:: + + >>> BlogPost.objects(id=post.id).update(title='Example Post') + >>> BlogPost.objects(id=post.id).update(set__title='Example Post') + .. note:: In version 0.5 the :meth:`~mongoengine.Document.save` runs atomic updates diff --git a/mongoengine/queryset/transform.py b/mongoengine/queryset/transform.py index fa97f8fe..3345ae64 100644 --- a/mongoengine/queryset/transform.py +++ b/mongoengine/queryset/transform.py @@ -164,6 +164,9 @@ def update(_doc_cls=None, **update): mongo_update.update(value) continue parts = key.split('__') + # if there is no operator, default to "set" + if len(parts) < 3 and parts[0] not in UPDATE_OPERATORS: + parts.insert(0, 'set') # Check for an operator and transform to mongo-style if there is op = None if parts[0] in UPDATE_OPERATORS: diff --git a/tests/document/instance.py b/tests/document/instance.py index 3ecbbec1..0903e3bb 100644 --- a/tests/document/instance.py +++ b/tests/document/instance.py @@ -1074,11 +1074,13 @@ class InstanceTest(unittest.TestCase): self.assertRaises(OperationError, update_no_value_raises) - def update_no_op_raises(): + def update_no_op_should_default_to_set(): person = self.Person.objects.first() person.update(name="Dan") + person.reload() + return person.name - self.assertRaises(InvalidQueryError, update_no_op_raises) + self.assertEqual("Dan", update_no_op_should_default_to_set()) def test_update_unique_field(self): class Doc(Document):