Clara
Clara

Reputation: 3035

Mongoengine - how to atomically update a dictionary?

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!

Upvotes: 2

Views: 1505

Answers (1)

Ross
Ross

Reputation: 18111

When every you do a $push or $pull from the friends field you will also in the same update have to modify the address_book eg:

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)

Upvotes: 3

Related Questions