2

I have a model that looks like this:

class TestDoc(Document):
    name = StringField()
    friends = ListField(StringField())
    address_book = DictField()

And I wish to atomically update the address_book field whenever the friends list changes. How can I modify both fields in an atomic operation? I can't find any modifier in the documentation:

https://mongoengine-odm.readthedocs.org/en/latest/guide/querying.html#atomic-updates

that makes atomic update for dictionaries. Thanks!

4

1 回答 1

3

当您每次执行$push$pull从该friends字段执行时,您还必须在同一更新中修改address_book例如:

TestDoc(name="Sue", friends=["Bob", "Sarah"], 
        address_book={"Bob": "1 the farm", "Sarah": "Owl house"}).save()

TestDoc.objects(name="Sue").update(pull__friends="Bob", 
                                   unset__address_book__Bob=1)
于 2013-04-19T10:02:20.057 回答