在 pymongo 中尝试将 $add 与 update_many (并使用 $project)一起使用时给出 'Unknow: modifier': $project

In pymongo while trying to use $add with update_many (and using $project) give 'Unknow: modifier': $project

我正在尝试对所有文档中存在的 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.

"update_many" 不接受与 "aggregate" 相同的运算符,请参阅 MongoDB 手册以获取当前实现的更新运算符列表:

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

无法使用更新运算符添加两个字段。您需要使用 "find" 来获取文档并在 Python 中添加字段,然后将具有 "update" 的字段“$set”为总和。