pymongo如何修改数组的许多元素,这是文档的关键

时间:2018-03-20 16:29:27

标签: python mongodb pymongo

我正在尝试更新mongodb文档中数组中的一些匹配元素。

我想将所有comments.$index.authorchao更新为tom,但我无法弄清楚语法。

这是我想在伪json中做的事情。

在:

{
 "author" : "lily",
 "contents" : "watch movive",
 "comments" : [
             {
                 "comment" : "good",
                 "author" : "chao",
                 "votes" : 0
             },
             {
                 "comment": "so so",
                 "author": "chao",
                 "votes": 3
             },
             ………………,
             {
                 "comment": "bad",
                 "author": "yong",
                 "votes": -1
             },
         ]
}

后:

{
"author" : "lily",
"contents" : "watch movive",
"comments" : [
             {
                 "comment" : "good",
                 "author" : "tom",
                 "votes" : 0
             },
             {
                 "comment": "so so",
                 "author": "tom",
                 "votes": 3
             },
             ………………,
             {
                 "comment": "bad",
                 "author": "yong",
                 "votes": -1
             },
         ]
 }

0 个答案:

没有答案