我想总结所有文档中的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
作为替代方案,我试图将$ project替换为$ set,但在这种情况下,我得到的错误是:
pymongo.errors.WriteError: The dollar ($) prefixed field '$add'
in 'interactions.$add' is not valid for storage.
答案 0 :(得分:0)
" update_many"不接受与"聚合"相同的运算符,请参阅MongoDB手册以获取当前实施的更新运算符列表:
https://docs.mongodb.com/manual/reference/operator/update/
无法使用更新运算符添加两个字段。您需要使用" find"获取文档并在Python中添加字段,然后" $ set"带有"更新"的字段总和。