Updating a list of embedded documents in mongoengi

2019-03-11 07:24发布

I'm struggling with mongoengine syntax.

I have the following models...

class Post(EmbeddedDocument):
    uid = StringField(required=True)
    text = StringField(required=True)
    when = DateTimeField(required=True)


class Feed(Document):
    label = StringField(required=True)
    feed_url = StringField(required=True)
    posts = ListField(EmbeddedDocumentField(Post))

    def my_method(self, post):
        pass

... and with the post object passed into to my_method, I'd like to update an existing post if it exists in self.posts with a matching uid, or push to self.posts if not.

Is there syntax to do that in one call in mongoengine?

2条回答
SAY GOODBYE
2楼-- · 2019-03-11 07:33

No with list field you cannot do an upsert into a list in a single query. $addToSet wont work as you've changed the post so you cant match. You can code round this but it does create a race condition where there is a small window of opportunity for error eg:

    class Post(EmbeddedDocument):
        uid = StringField(required=True)
        text = StringField(required=True)

    class Feed(Document):
        label = StringField(required=True)
        feed_url = StringField(required=True)
        posts = ListField(EmbeddedDocumentField(Post))

    Feed.drop_collection()

    Feed(
        label="label",
        feed_url="www.feed.com"
    ).save()

    post = Post(uid='1', text="hi")
    updated = Feed.objects(posts__uid=post.uid).update_one(set__posts__S=post)
    if not updated:
        Feed.objects.update_one(push__posts=post)

First we try to update and if it doesn't exist we push to the list - this is where there is a window of opportunity for another process to run and potentially push the post on the list.

The risk might be acceptable but realistically, I think changing your schema is better, potentially splitting Post out into its own collection. Then you can use an update statement and set the whole object. The cost will be an extra query to get the feed data.

查看更多
Ridiculous、
3楼-- · 2019-03-11 07:39
Feed.objects.filter(posts__uid=post.uid).\
          update_one(push__posts__S__comments='comment demo')
查看更多
登录 后发表回答