'Pymongo@MongoDB Atlas TypeError: 'Collection' object is not callable

I am trying to use Pymongo to store data to MongoDB Atlas.

When deploying from a Windows 10 environment to Ubuntu Server, I get the following error.

It worked fine on Windows, so why is this happening?

from itemadapter import ItemAdapter
# import collections
import pymongo

class MongoPipeline:

    collection_name = 'all'

    def open_spider(self, spider):
        self.client = pymongo.MongoClient('mongodb+srv://xxxx:[email protected]/myFirstDatabase?retryWrites=true&w=majority')
        self.db = self.client['ShopDB']

    def close_spider(self, spider):
        self.client.close()

    def process_item(self, item, spider):
        self.db[self.collection_name].insert(dict(item)) # insert(dict(item))



Traceback (most recent call last):
  File "/home/don/.local/lib/python3.8/site-packages/twisted/internet/defer.py", line 857, in _runCallbacks
    current.result = callback(  # type: ignore[misc]
  File "/home/don/.local/lib/python3.8/site-packages/scrapy/utils/defer.py", line 162, in f
    return deferred_from_coro(coro_f(*coro_args, **coro_kwargs))
  File "/home/don/py/men_kutikomi/kutikomi/pipelines.py", line 24, in process_item
    self.db[self.collection_name].insert(dict(item)) # insert(dict(item))
  File "/home/don/.local/lib/python3.8/site-packages/pymongo/collection.py", line 2779, in __call__
    raise TypeError(
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.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source