Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 318
Jason Strimpel Avatar asked Oct 15 '22 15:10

Jason Strimpel


People also ask

Is 'collection' object callable in pymongo-4?

I am using Pymongo-4 and did not know that the project implicitly required an older version of Pymongo, namely version 3. TypeError: '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. The error message was a straightforward explanation.

What does TypeError collection object is not callable mean?

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. import pymongo import praw import time def main (): fpid = os.fork () if fpid!=0: # Running as daemon now.

Why is the 'insert_one' method failing in pymongo?

If you meant to call the 'insert_one' method on a 'Collection' object it is failing because no such method exists. import pymongo import praw import time def main (): fpid = os.fork () if fpid!=0: # Running as daemon now.

What does it mean when a Python object is not callable?

The TypeError object is not callable is raised by the Python interpreter when an object that is not callable gets called using parentheses. This can occur, for example, if by mistake you try to access elements of a list by using parentheses instead of square brackets.


2 Answers

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.

The 2.8 release tutorial points to the .insert() method instead:

posts.insert({'a':1})

Since .insert_one() is only available in the 3.0b driver.

Either force the installation of the "beta" driver or live with a stable driver and the available methods.

This seems to be the fault of the current "search engine response" matching the "beta release" as "current".

like image 64
Neil Lunn Avatar answered Oct 18 '22 03:10

Neil Lunn


The problem is that you are following the tutorial from the current release documentation but actually have PyMongo 2.8 installed.

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.

You can install or upgrade your driver using pip like.

python -m pip install -U pymongo
like image 20
styvane Avatar answered Oct 18 '22 03:10

styvane