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

TypeError: ‘Collection‘ object is not callable. If you meant to call the ‘insert‘ method on a ‘DatabfrompymongoimportMongoClient报错问题描述解决方法问题描述使用pymongo连接本地的MongoDB,跟个老师的视频,出现报错,后查询资料改正frompymongoimportMongoClient#创建数据库连接对象client=MongoClient()#选择一个数据库db=client[‘python’]#身份认证#db.authenticate(‘python’,’python’)#选择一个集合col=c

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

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.net/160505.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月16日 下午7:16
下一篇 2022年7月16日 下午7:16


相关推荐

  • Spring Cloud 学习笔记(2 / 3)

    Spring Cloud 学习笔记(2 / 3)SpringCloud学习笔记(1/3)SpringCloud学习笔记(3/3)—56_Hystrix之全局服务降级DefaultProperties57_Hystrix之通配服务降级FeignFallback58_Hystrix之服务熔断理论59_Hystrix之服务熔断案例(上)60_Hystrix之服务熔断案例(下)61_Hystrix之服务熔断总结62_Hystrix工作流程最后总结63_Hystrix图形化Dashboard搭建

    2022年6月2日
    36
  • xshell的安装和使用_Xshell命令

    xshell的安装和使用_Xshell命令Xshell安装Xshell安装完后提示更新到最新版本

    2025年10月10日
    5
  • 项目部署、配置、查错常用到的Linux命令

    项目部署、配置、查错常用到的Linux命令

    2020年11月12日
    261
  • java interface 介绍

    java interface 介绍interface 类似于 class 只不过 interface 里的所有方法都是 abstract 抽象的 当一个非抽象的 class 实现 implements 一个接口 interface 时 必须实现接口中所有方法 因为都是 abstract 抽象的 否则该 class 必须声明为 abstract sample1 interfaceusa publicclass

    2026年3月26日
    2
  • JAVA POI的使用

    JAVA POI的使用下载地址 https archive apache org dist poi release bin 需要的 jar 包 我用的是 3 10final Poi 3 10 Final jar 用于 xls Poi ooxml 3 10 Final jar 用于 xlsx Poi ooxml schemas 3 10 jarXmlbeans 2 30 jardom4j 1 6 1 jarp

    2025年11月1日
    7
  • Omnibus GitLab 使用说明

    Omnibus GitLab 使用说明1 OmnibusGitLa 使用说明 gt 参考 https gitlab com gitlab org omnibus gitlab blob master README md backup and restore omnibus gitlab configuratio 1 说明该项目为 GitLab 创建了特定于平台的平台特有的可下载软件包 https about gitlab

    2026年3月18日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号