Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Collection' object is not callable for both insert() and insert_one() method of a pymongo Database

getting a really roadblocking error in pymongo here, which prevents me from proceeding in a project. I have searched this case but other similar posts and their answers did not work for me.

First of all, I'm running:

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

This is my minimal working / error producing example:

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.
>>>

Other StackOverflow topic have suggested using insert_one() instead. However, this yields the same result for me:

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.

Any help is appreciated!

like image 256
J. Becker Avatar asked Jul 23 '18 22:07

J. Becker


1 Answers

Try:

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
like image 95
Joe Drumgoole Avatar answered Sep 30 '22 09:09

Joe Drumgoole