簡體   English   中英

Pymongo更新與Mongo Shell不同

[英]Pymongo Updating Differently than Mongo Shell

在使用Pymongo進行更新時,如果將相同的查詢放入外殼程序(其中db=blog ),則不會得到結果(或錯誤)

在外殼中,這有效:

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

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

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

我也嘗試過comments.[0].num_likes在傳單上...

日志中未報告任何錯誤,並且由於觸摸的文檔數是正確的,因此此更新返回1時,該數據尚未更改。

其他未編入索引的更新在這兩種方法中均有效。

我在這里想念什么?

謝謝!

記住, blog.posts中的blog.posts是字符串,但是在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}})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM