zl程序教程

您现在的位置是:首页 >  其他

当前栏目

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

On not to The is if object call
2023-06-13 09:12:26 时间

大家好,又见面了,我是你们的朋友全栈君。

from pymongo import MongoClient 报错

问题描述

使用pymongo 连接本地的MongoDB,跟个老师的视频,出现报错,后查询资料改正

from pymongo import MongoClient

# 创建数据库连接对象
client = MongoClient()

# 选择一个数据库
db = client['python']

# 身份认证
# db.authenticate('python', 'python')

# 选择一个集合
col = client['stu']

col.insert({ 
   'a': 'b'})
'''

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

解决方法

from pymongo import MongoClient

# 创建数据库连接对象
client = MongoClient()

# 选择一个数据库
db = client['python']

# 身份认证
# db.authenticate('python', 'python')

# 选择一个集合
col = db['stu']

# 插入一条数据
col.insert({ 
   'a': 'b'})

更改一下,使用数据库获取集合:

# 选择一个集合
col = db['stu']

参考官方文档

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/160505.html原文链接:https://javaforall.cn