如何在mongoengine中删除文档的另一个嵌入文档中的嵌入文档项

时间:2015-04-27 12:14:09

标签: python mongoengine flask-mongoengine

我的模特:

class City(EmbeddedDocument):
    name = StringField()
    population = IntField()

class Privince(EmbeddedDocument):
    name = StringField()
    cities = ListField(EmbeddedDocumentField(City))

class State(Document):
    name = StringField()
    provinces = ListField(EmbeddedDocumentField(Privince))

我可以删除此代码的状态Province

State.objects(name="Alaska").update_one(pull__provinces={"name": "Juneau"})

但我不知道如何删除province的城市。

1 个答案:

答案 0 :(得分:0)

哦,我找到了一个非常简单的方法! :

state_obj = State.objects.get(name="Alaska")
province_obj = state_obj.provinces({"name": "Juneau"})
for city in province_obj.cities:
    if city.name == "Test_City" and city.population == 10000:
        province_obj.cities.remove(city)