问题
I have a class mongoengine
class Post(EmbeddedDocument):
uid = StringField(required=True)
text = StringField(required=True)
value = StringField()
class Feed(Document):
label = StringField(required=True)
feed_url = StringField(required=True)
posts = ListField(EmbeddedDocumentField(Post))
I am trying update a element in Feed(posts) The first: I get a object Feed
model = Feed.objects(_id="....").first()
Continue I want update Post in model have text = "title".
How can I do it with mongoengine? Thanks
回答1:
I resolved it :)
Feed.objects(_id="...", posts__text="findvalue").update(set__posts__S__value="updatevalue")
回答2:
If I understand the question, I think something like this will work:
model = Feed.objects(_id="....").first()
for post in model.posts:
if post.text == "title":
post.value = "placeholder for real update"
model.save()
来源:https://stackoverflow.com/questions/14170479/updating-a-embedded-documents-in-mongoengine