鱼C论坛

 找回密码
 立即注册
查看: 1275|回复: 8

[已解决]python中Mongo插入数据问题

[复制链接]
发表于 2023-6-27 09:35:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import pymongo
  2. client = pymongo.MongoClient(host='localhost',port=27017)
  3. student = {
  4.     'id':'20170101',
  5.     'name':'Jordan',
  6.     'age':20,
  7.     'gender':'male'
  8. }
  9. db = client.test
  10. collection = db.students
  11. result = collection.insert(student)
  12. print(result)
复制代码


代码:
  1. result = collection.insert(student)
复制代码


'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.
报错了,是什么问题呢?
最佳答案
2023-6-27 09:36:50
这个错误是因为在 pymongo 的版本 3.0 之后,insert 方法已经被弃用,取而代之的是 insert_one 或 insert_many 方法。所以你需要将代码中的 collection.insert(student) 修改为 collection.insert_one(student)。

修改后的代码如下:

  1. import pymongo

  2. client = pymongo.MongoClient(host='localhost', port=27017)
  3. student = {
  4.     'id': '20170101',
  5.     'name': 'Jordan',
  6.     'age': 20,
  7.     'gender': 'male'
  8. }
  9. db = client.test
  10. collection = db.students
  11. result = collection.insert_one(student)
  12. print(result)
复制代码

这样就可以正常插入数据到 MongoDB 数据库中了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-27 09:36:50 | 显示全部楼层    本楼为最佳答案   
这个错误是因为在 pymongo 的版本 3.0 之后,insert 方法已经被弃用,取而代之的是 insert_one 或 insert_many 方法。所以你需要将代码中的 collection.insert(student) 修改为 collection.insert_one(student)。

修改后的代码如下:

  1. import pymongo

  2. client = pymongo.MongoClient(host='localhost', port=27017)
  3. student = {
  4.     'id': '20170101',
  5.     'name': 'Jordan',
  6.     'age': 20,
  7.     'gender': 'male'
  8. }
  9. db = client.test
  10. collection = db.students
  11. result = collection.insert_one(student)
  12. print(result)
复制代码

这样就可以正常插入数据到 MongoDB 数据库中了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-27 09:37:16 | 显示全部楼层
本帖最后由 sfqxx 于 2023-6-27 09:43 编辑

该报错是因为pymongo 4.x版本中的insert方法已被移除,不再可用。相反,应使用`insert_one`或`insert_many`方法插入文档。

对于你的代码,在插入单个文档时,应将`collection.insert(student)`更改为`collection.insert_one(student)`:


  1. result = collection.insert_one(student)
复制代码


这是代码:
  1. import pymongo

  2. client = pymongo.MongoClient(host='localhost', port=27017)
  3. student = {
  4.     'id': '20170101',
  5.     'name': 'Jordan',
  6.     'age': 20,
  7.     'gender': 'male'
  8. }
  9. db = client.test
  10. collection = db.students
  11. result = collection.insert_one(student)
  12. print(result)
复制代码


或者:
  1. import pymongo
  2. client = pymongo.MongoClient(host='localhost',port=27017)
  3. students = [
  4.     {'id':'20170101', 'name':'Jordan', 'age':20, 'gender':'male'},
  5.     {'id':'20170102', 'name':'Mike', 'age':21, 'gender':'male'}
  6. ]
  7. db = client.test
  8. collection = db.students
  9. result = collection.insert_many(students)
  10. print(result.inserted_ids)
复制代码


这样就可以正确执行插入操作了。希望能帮到你!

给个最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-27 09:37:19 | 显示全部楼层
你的报错信息显示  '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. ,这是因为在较新的pymongo版本中, insert  方法已经被弃用,取而代之的是  insert_one  或  insert_many 。

你可以根据需要选择适合的方法。如果你只插入一条数据,可以使用  insert_one  方法;如果插入多条数据,可以使用  insert_many  方法。

你的代码可以改写为如下:

  1. import pymongo
  2. client = pymongo.MongoClient(host='localhost',port=27017)
  3. student = {
  4.     'id':'20170101',
  5.     'name':'Jordan',
  6.     'age':20,
  7.     'gender':'male'
  8. }
  9. db = client.test
  10. collection = db.students
  11. result = collection.insert_one(student)
  12. print(result.inserted_id)
复制代码


这段代码的最后一行会输出插入文档的  _id 。如果文档中没有提供  _id ,那么  insert_one()  会自动为其生成一个。

如果你需要一次插入多条数据,可以这样写:

  1. import pymongo
  2. client = pymongo.MongoClient(host='localhost',port=27017)
  3. students = [
  4.     {'id':'20170101', 'name':'Jordan', 'age':20, 'gender':'male'},
  5.     {'id':'20170102', 'name':'Mike', 'age':21, 'gender':'male'}
  6. ]
  7. db = client.test
  8. collection = db.students
  9. result = collection.insert_many(students)
  10. print(result.inserted_ids)
复制代码


这段代码会输出所有插入文档的  _id  列表。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-27 09:40:38 | 显示全部楼层
isdkz 发表于 2023-6-27 09:37
你的报错信息显示  'Collection' object is not callable. If you meant to call the 'insert' method on a ...

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-27 09:55:54 | 显示全部楼层
歌者文明清理员 发表于 2023-6-27 09:36
这个错误是因为在 pymongo 的版本 3.0 之后,insert 方法已经被弃用,取而代之的是 insert_one 或 insert_m ...

<pymongo.results.InsertOneResult object at 0x000002C768D77FD0>

这条数据提示这个错误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-27 09:56:36 | 显示全部楼层
python小小白哟 发表于 2023-6-27 09:55
这条数据提示这个错误

这个不是错误,你看他的字母:

<pymongo.results.InsertOneResult object at 0x000002C768D77FD0>

这是他返回的结果
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-27 10:42:18 | 显示全部楼层
歌者文明清理员 发表于 2023-6-27 09:56
这个不是错误,你看他的字母:

完了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-27 10:42:45 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 16:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表