我的模特:
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
的城市。
答案 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)