简体   繁体   English

集合 object 不是 PyMongo 的可调用错误

[英]Collection object is not callable error with PyMongo

Following along the PyMongo tutorial and 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?关于我在这里做错了什么的任何指导?

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. 这是一个明确的问题,但这里的问题似乎是你正在阅读“beta”发布文档,但很可能你实际上最多安装了“pymongo”2.8而不是链接中提到的“3.0b”引用。

The 2.8 release tutorial points to the .insert() method instead: 2.8版本教程指向.insert()方法:

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". 这似乎是当前“搜索引擎响应”的错误,将“beta版本”与“当前”匹配。

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 . insert_one()方法是PyMongo 3.0中的新方法,现在在PyMongo 2.9中向后移植。 So clearly you will need to install PyMongo 2.9 or newer version in order to use the new API feature. 很明显,您需要安装PyMongo 2.9或更新版本才能使用新的API功能。

You can install or upgrade your driver using pip like. 您可以使用pip来安装或升级驱动程序。

python -m pip install -U pymongo

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-packages folder due to insufficient permission privileges. 显然,在我的发行版上,由于权限不足,安装程序无法在dist-packages文件夹中创建库。 So, I solved the problem by granting it write permissions and re-installing the PyMongo driver: 所以,我通过授予写权限并重新安装PyMongo驱动程序来解决问题:

cd /usr/local/lib/python2.7/
sudo chmod 0777 dist-packages
pip install -U pymongo

Hope this helps. 希望这可以帮助。

I got this error when calling update on a collection.在集合上调用更新时出现此错误。 It's now:下雪了:

from pymongo import MongoClient

db = MongoClient('localhost', 27017).my_db_name
db.my_collection_name.update_one( ... )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM