gpt4 book ai didi

python - pymongo 数据库的 insert() 和 insert_one() 方法都不能调用“Collection”对象

转载 作者:行者123 更新时间:2023-11-28 20:33:02 24 4
gpt4 key购买 nike

pymongo 中遇到一个真正的障碍错误,这使我无法继续进行项目。我已经搜索过这个案例,但其他类似的帖子和他们的答案对我不起作用。

首先,我在运行:

mongod v3.6.5-2-g9b2264cf14    
MongoDB shell version v3.6.5-2-g9b2264cf14

这是我的最小工作/错误生成示例:

myname@myhost ~ $ /usr/bin/python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>> db = pymongo.MongoClient("localhost:27017")
>>> db.testcollection.insert({"foo": "bar"})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/pymongo/collection.py", line 2344, in __call__
self.__name)
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.
>>>

其他 StackOverflow 主题建议改用 insert_one()。但是,这对我产生了相同的结果:

myname@myhost ~ $ /usr/bin/python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>> db = pymongo.MongoClient("localhost:27017")
>>> db.testcollection.insert_one({"foo": "bar"})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/pymongo/collection.py", line 2344, in __call__
self.__name)
TypeError: 'Collection' object is not callable. If you meant to call the 'insert_one' method on a 'Database' object it is failing because no such method exists.

感谢任何帮助!

最佳答案

尝试:

import pymongo
client = pymongo.MongoClient()
db = client[ "testdb" ] # makes a test database called "testdb"
col = db[ "testcol" ] #makes a collection called "testcol" in the "testdb"
col.insert_one( {"foo" : "bar" }) #add a document to testdb.testcol

关于python - pymongo 数据库的 insert() 和 insert_one() 方法都不能调用“Collection”对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51488127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com