当前位置: 代码迷 >> 综合 >> TypeError: ‘Collection‘ object is not callable. If you meant to call the ‘authenticate‘ method
  详细解决方案

TypeError: ‘Collection‘ object is not callable. If you meant to call the ‘authenticate‘ method

热度:34   发布时间:2023-12-15 17:22:39.0

使用pymongo连接MongoDB用户认证

self.client = pymongo.MongoClient(host="127.0.0.1", port=27017)
self.client["admin"].authenticate("admin", "12345678")

在3.9版本及以前是正常运行的,而在4.0版本出错:

TypeError: 'Collection' object is not callable. If you meant to call the 'authenticate' method on a 'Database' object it is failing because no such method exists.

 原因是4.0版本做了修改

4.0文档地址:PyMongo 4 Migration Guide — PyMongo 4.0.1 documentation

 

解决办法:

更新代码

client = pymongo.MongoClient(host="127.0.0.1", port=27017,username="admin",password="12345678")

或者退回到4.0之前的版本

先使用

pip uninstall pymongo

输入y进行删除

然后再次安装时指定pymongo的版本为3.9

pip install pymongo==3.9

 

  相关解决方案