【发布时间】:2015-09-23 06:32:56
【问题描述】:
我有一个模特 -
class Comment(EmbeddedDocument):
c_id = Integer()
content = StringField()
class Page(DynamicDocument):
comments = ListField(EmbeddedDocumentField(Comment))
我插入以下数据
comment1 = Comment(c_id=1,content='Good work!')
comment2 = Comment(c_id=2,content='Nice article!')
page = Page(comments=[comment1, comment2])
现在我想将id为1的评论更新为Great work!。我该怎么做?
我在一些 SO 线程上读到它可以通过以下方式完成:-
p_obj = Page.objects.get(comments__c_id=1).update(set__comments__S__content='Great work')
但是,上面的更新会抛出一个错误:-
Update failed (Cannot apply the positional operator without a corresponding query field containing an array.)
以下是文档结构:-
{
"comments": [{
"content": "Good Work",
"c_id": "1"
},
{
"content": "Nice article",
"c_id": "2"
}],
}
【问题讨论】:
标签: python mongodb mongodb-query mongoengine