Updating a embedded documents in mongoengine

我是研究僧i 提交于 2019-12-21 05:52:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!