MongoEngine怎样更新列表数据到MongoDB


mongodb数据结构是下面这样的:

{
    "_id" : 1,
    "tag" : [tag1, tag2, tag3, tag4]
}

然后添加 tag5 tag

post = Post.objects(id=1).first()   # 获取所在post
temp_tag = post.tag                 # 获取post的tag列表,不知道问题是不是出在这?
post.tag = temp_tag.append('tag5')  # 添加tag5到列表末尾
post.save()                         # 更新

但是怎么都添加不进去,是不是我的写法有问题?

mongoengine python flask mongodb

﹀ァ亂髮ヤ 11 years, 1 month ago

 post.tag = temp_tag.append('tag5')  # 添加tag5到列表末尾

这是在干嘛? append 原地修改,应该返回None吧。

昵称什么的…… answered 11 years, 1 month ago

Your Answer