我使用MongoDB作为Python Web应用程序的后端数据库(PyMongo + Bottle).用户可以上传文件,并可选择在上传过程中"标记"这些文件.标签作为列表存储在文档中,如下所示:
{
"_id" : ObjectId("561c199e038e42b10956e3fc"),
"tags" : [ "tag1", "tag2", "tag3" ],
"ref" : "4780"
}
Run Code Online (Sandbox Code Playgroud)
我试图允许用户将新标签附加到任何文档.我提出了这样的事情:
def update_tags(ref, new_tag)
# fetch desired document by ref key as dict
document = dict(coll.find_one({'ref': ref}))
# append new tag
document['tags'].append(new_tag)
# re-insert the document back into mongo
coll.update(document)
Run Code Online (Sandbox Code Playgroud)
(fyi; refkey总是唯一的.这也很容易_id.)似乎应该有一种方法可以直接更新'tags'值而不需要拉回整个文档并重新插入.我在这里错过了什么吗?
任何想法都非常感谢:)