1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > TypeError: ‘Collection‘ object is not callable. If you meant to call the ‘insert‘ method on a ‘Colle

TypeError: ‘Collection‘ object is not callable. If you meant to call the ‘insert‘ method on a ‘Colle

时间:2021-01-04 15:35:05

相关推荐

TypeError: ‘Collection‘ object is not callable. If you meant to call the ‘insert‘ method on a ‘Colle

在学习mongoDB时,遇到了以下报错,查阅资料后解决了,希望对正在学习的小伙伴如遇到此类问题能有所帮助。TypeError: 'Collection' object is not callable. If you meant to call the 'insert' method on a 'Collection' object it is failing because no such method exists.

下面看问题:

代码如下(示例):

from pymongo import MongoClient

def get_db():client= MongoClient(host="localhost", port=27017)#默认端口号:27017db = client["sylar"]return dbdef add_one(data):# 拿到dbdb = get_db()result = db['user'].insert({"name": "小红", "age": 18})return resultif __name__ == '__main__':add_one(1)

运行后出现下面的错误:

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

我们只需要将上面的" result = db['user'].insert({"name": "小红", "age": 18})" 代码中的".insert"改为".insert_one"即可,如下:

result = db['user'].insert_one({"name": "小红", "age": 18})

再次运行将不报错。

在windows Powershell 窗口进行查询,如下图数据已存在。

到这里问题就解决啦。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。