【问题标题】:Pymongo Updating Differently than Mongo ShellPymongo 更新方式与 Mongo Shell 不同
【发布时间】:2013-11-04 22:29:26
【问题描述】:

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

在 shell 中,这是有效的:

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

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

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

我还在传单上尝试过comments.[0].num_likes...

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

其他非索引更新在两者中都有效。

我在这里错过了什么?

谢谢!

【问题讨论】:

    标签: python mongodb pymongo


    【解决方案1】:

    记住,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}})
    

    【讨论】:

    • 虽然是这样,但 db.posts 已在 Python 代码中作为参数正确传递。
    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 2015-10-25
    • 2015-11-18
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多