跟随 PyMongo tutorial在集合上调用 insert_one
方法时出现错误。
In [1]: import pymongo
In [2]: from pymongo import MongoClient
In [3]: client = MongoClient()
In [4]: db = client.new_db
In [5]: db
Out[5]: Database(MongoClient('localhost', 27017), u'new_db')
In [6]: posts = db.posts
In [7]: posts.insert_one({'a':1})
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-2271c01f9a85> in <module>()
----> 1 posts.insert_one({'a':1})
C:\Anaconda\lib\site-packages\pymongo-2.8-py2.7-win32.egg\pymongo\collection.py in __call__(self, *a
rgs, **kwargs)
1771 "call the '%s' method on a 'Collection' object it is "
1772 "failing because no such method exists." %
-> 1773 self.__name.split(".")[-1])
TypeError: 'Collection' object is not callable. If you meant to call the 'insert_one' method on a 'Collection' object it is failing because no such method exists.
网上有一些帖子讨论了这个错误,但似乎都是当用户调用一个已弃用的名称时。
关于我在这里做错了什么的任何指导?
最佳答案
这是一个明确的问题,但这里的问题似乎是您正在阅读 "beta"发布文档,但很可能您实际上最多安装了“pymongo”2.8,而不是您引用的链接中提到的“3.0b”。
2.8 release tutorial指向.insert()
替代方法:
posts.insert({'a':1})
自从 .insert_one()
仅在 3.0b 驱动程序中可用。
要么强制安装“测试版”驱动程序,要么使用稳定的驱动程序和可用的方法。
这似乎是当前“搜索引擎响应”匹配“测试版”为“当前”的错误。
https://stackoverflow.com/questions/28981718/