2017-09-26 101 views
0

我想總結出現在所有文檔中的2個字段(favorite_count和retweet_count。)然後添加結果作爲一個新的領域(或更新):

filter = {'user.screen_name':author} 
db.dwh_twt_tweets.update_many(filter= filter, update= {"$project":{ 
            'favorite_count':'$favorite_count', 
            'retweet_count':'$retweet_count', 
            'interactions':{"$add": 
              ['$favorite_count','$retweet_count']} 
               } 
               } 
              ) 

我得到:

raise WriteError(error.get("errmsg"), error.get("code"), error) 
pymongo.errors.WriteError: Unknown modifier: $project 

作爲替代我試圖替換爲$項目$設置,但在這種情況下,錯誤,我得到是:

pymongo.errors.WriteError: The dollar ($) prefixed field '$add'  
in 'interactions.$add' is not valid for storage. 
+0

剛刪除(filter =,update =),同樣的錯誤 –

回答

0

「update_many」不接受同一運營商的「聚合」,請參閱MongoDB的手冊當前實施的更新運營商列表:

https://docs.mongodb.com/manual/reference/operator/update/

這是不可能的使用更新運算符添加兩個字段。您需要使用「find」來獲取文檔並在Python中添加字段,然後「$ set」字段中的「update」爲總和。

相關問題