I am working on a project that explicitly requires the insert
command for creating a CRUD class using Python. I am using Pymongo-4 and did not know that the project implicitly required an older version of Pymongo, namely version 3.
This is how the insert
method was used:
self.database.animals.insert(data)
The result of using insert
on a newer version of Pymongo issued the following error message:
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. It says that the collection does not have a method called insert
.
I was not sure what I needed to do at first, but the Pymongo changelog confirmed that insert
is deprecated as of version 3.
The following methods are deprecated:
- save()
- insert()
- update()
- remove()
- find_and_modify()
- ensure_index()
My options were to use insert_one
, insert_many
, or downgrade my version of Pymongo below version 3. I went with the latter since I was required to use the insert
command.
Conclusion
The solution for “TypeError: ‘Collection’ object is not callable” is to use a version of Pymongo that supports the insert
method. Alternatively, you can use insert_one
or insert_many
if you wish to use a newer version of Pymongo.