2

在使用 Pymongo 进行更新时,如果将相同的查询放入 shell 中,我不会得到结果(或错误),其中db=blog

在外壳中,这有效:

db.posts.update(
    {'permalink': 'TLxrBfyxTZjqOKqxgnUP'},
    {'$inc': {'comments.0.num_likes':1}});

在 Pymongo where 中self.posts = blog.posts,以下内容不起作用

self.posts.update({'permalink': 'TLxrBfyxTZjqOKqxgnUP'},
      {'$inc': {'comments.0.num_likes':1}})

我也试过comments.[0].num_likes传单...

日志中没有报告错误,虽然此更新返回 1 作为触摸的文档数,这是正确的,但该数据尚未更改。

其他非索引更新确实适用于两者。

我在这里想念什么?

谢谢!

4

1 回答 1

0

请记住,blog.posts在 shell 中是字符串,但在 python 中是变量。

尝试:

from pymongo import MongoClient
con = MongoClient("mongodb://user:pass@127.0.0.1:"+port+"/blog")
db=con['blog']
db['posts'].update({'permalink': 'TLxrBfyxTZjqOKqxgnUP'},
                   {'$inc': {'comments.0.num_likes':1}})
于 2013-10-28T15:55:58.713 回答