我有一个模型,看起来像这样:
class TestDoc(Document):
name = StringField()
friends = ListField(StringField())
address_book = DictField()
并且我希望在好友列表更改时自动更新address_book字段。如何在原子操作中修改这两个字段?我在文档中找不到任何修饰符:
https://mongoengine-odm.readthedocs.org/en/latest/guide/querying.html原子更新
对字典进行原子更新。谢谢!
当您每次从friends
字段执行$push
或$pull
时,您也将在同一更新中修改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)