Collection' 对象不可调用。如果您打算在“Collection”对象上调用“update”方法,则会失败,因为不存在这样的方法

Sid*_*sha 6 pymongo

我正在尝试使用 pymongo 将记录保存到 mongo db 中。我有一个脚本来更新集合(如果存在)或插入一条记录。此代码片段在交互运行时有效,但当我尝试从 docker 执行相同操作时,脚本失败并显示错误 - Collection' object is not callable。如果您打算对“Collection”对象调用“update”方法,则会失败,因为不存在此类方法。

    def store_failed_cbsd(id,data,collection_name,mongo_client):      
      output_content = {'_id' : id,'data' : [data]}   
      key = {'_id': id}    
      mongo_client.get_database()[collection_name].update(key,    
      output_content, upsert=True)
Run Code Online (Sandbox Code Playgroud)

PyMongo 版本 - 4.1.1 我缺少什么?

Bel*_*ter 9

update()pymongo 4 已弃用。使用update_many()或update_one()代替。

https://pymongo.readthedocs.io/en/stable/migrate-to-pymongo4.html#collection-update-is-removed