What is the proper way to update a listfield of embedded documents in mongoengine?

后端 未结 2 395
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 10:25

I am trying to define methods for performing checks and updates to a listfield of embedded documents in mongoengine. What is the proper way of doing what I\'m trying to do. The

2条回答
  •  梦如初夏
    2021-02-06 10:50

    You need to find the index of the existing comment.

    You can then overwrite the old comment with the new comment (where i is the index) eg:

    post.comments[i] = new_comment
    

    then just do a post.save() and mongoengine will convert that to a $set operation.

    Alternatively, you could just write the $set eg:

    Post.objects(pk=post.pk).update(set__comments__i=comment)
    

提交回复
热议问题