Python PyMongo 的集合对象不可调用错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28981718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Collection object is not callable error with PyMongo
提问by Jason Strimpel
Following along the PyMongo tutorialand am getting an error when calling the insert_one
method on a collection.
按照 PyMongo教程进行操作,在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.
There are a few posts online that discuss this error but all seem to be when the user calls a deprecated name.
网上有一些帖子讨论了这个错误,但似乎都是当用户调用一个不推荐使用的名称时。
Any guidance on what I am doing wrong here?
关于我在这里做错了什么的任何指导?
采纳答案by Neil Lunn
It is a clear question but the problem here seems to be that you are reading from the "beta"release documentation but in all likelihood you actually at most have "pymongo" 2.8 installed rather than the "3.0b" referred to in the link you quote.
这是一个明确的问题,但这里的问题似乎是您正在阅读“测试版”发布文档,但很可能您实际上最多安装了“pymongo”2.8,而不是链接中提到的“3.0b”引用。
The 2.8 release tutorialpoints to the .insert()
method instead:
posts.insert({'a':1})
Since .insert_one()
is only available in the 3.0b driver.
因为.insert_one()
仅在 3.0b 驱动程序中可用。
Either force the installation of the "beta" driver or live with a stable driver and the available methods.
要么强制安装“beta”驱动程序,要么使用稳定的驱动程序和可用的方法。
This seems to be the fault of the current "search engine response" matching the "beta release" as "current".
这似乎是当前“搜索引擎响应”匹配“测试版”为“当前”的故障。
回答by styvane
The problem is that you are following the tutorial from the current release documentation but actually have PyMongo 2.8 installed.
问题是您正在遵循当前版本文档中的教程,但实际上安装了 PyMongo 2.8。
The insert_one()
method is new in PyMongo 3.0 now backported in PyMongo 2.9. So clearly you will need to install PyMongo 2.9 or newer version in order to use the new API feature.
该insert_one()
方法是 PyMongo 3.0 中的新方法,现在在PyMongo 2.9中反向移植。很明显,您需要安装 PyMongo 2.9 或更新版本才能使用新的 API 功能。
You can install or upgrade your driver using pip
like.
您可以使用pip
like安装或升级您的驱动程序。
python -m pip install -U pymongo
回答by Siddharth Karnam
I was facing the same problem too. When I tried upgrading my PyMongo distribution using the command,
我也面临同样的问题。当我尝试使用命令升级我的 PyMongo 发行版时,
pip install -U pymongo
I got the following error :
我收到以下错误:
error: could not create '/usr/local/lib/python2.7/dist-packages/pymongo': Permission denied
错误:无法创建“/usr/local/lib/python2.7/dist-packages/pymongo”:权限被拒绝
Apparently, on my distro, the installer was not able to create a library in the dist-packagesfolder due to insufficient permission privileges. So, I solved the problem by granting it write permissions and re-installing the PyMongo driver:
显然,在我的发行版上,由于权限不足,安装程序无法在dist-packages文件夹中创建库。所以,我通过授予它写权限并重新安装 PyMongo 驱动程序解决了这个问题:
cd /usr/local/lib/python2.7/
sudo chmod 0777 dist-packages
pip install -U pymongo
Hope this helps.
希望这可以帮助。