Click here to Skip to main content
15,667,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi i am trying to store a text file in mangoDb. the text file has data of some generated hashes. but i am receiving this error when i try to store file.
 "failing because no such method exists." % self.__name.split(".")[-1]
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.

 from pymongo import MongoClient
client = MongoClient()
db = client.test_database  # use a database called "test_database"
collection = db.files   # and inside that DB, a collection called "files"

f = open('hashes.txt')  # open a file

# build a document to be inserted
text_file_doc = {"file_name": "hashes.txt"}
# insert the contents into the "file" collection
What I have tried:

i have tried above code .but got errors i aslo tried it with insert_one and insert_many but still errors.

What I have tried:

I have tried above code but getting errors. i aslo tried insert_one and insert_many but not useful .
Updated 12-Jun-22 6:20am
Richard MacCutchan 12-Jun-22 8:57am    
The error message you have shown does not contain a reference to the code you have posted.
Ayesha Tassaduq 12-Jun-22 10:12am    
Full error message
File "C:/Users/DELL/PycharmProjects/ourfirstpycharmproject/", line 124, in <module>
File "C:\Users\DELL\PycharmProjects\ourfirstpycharmproject\venv\lib\site-packages\pymongo\", line 675, in insert_many
raise TypeError("documents must be a non-empty list")
TypeError: documents must be a non-empty list
Richard MacCutchan 12-Jun-22 10:51am    
Have you checked that your MongoDB instance is running, as described at Tutorial — PyMongo 4.1.1 documentation[^]?

See also Tutorial — PyMongo 4.1.1 documentation[^].
Ayesha Tassaduq 12-Jun-22 12:03pm    
is there any other way to store a text file ?
Richard MacCutchan 12-Jun-22 12:29pm    
Yes, many ways. It all depends on what you are trying to achieve.

1 solution

I just tested this with the MongoDB server running and it works correctly. You need to make sure you have a server instance running on your system.
Share this answer
Ayesha Tassaduq 12-Jun-22 16:03pm    
ok i am trying to do it.. will tell you of if i succeed
Ayesha Tassaduq 12-Jun-22 17:36pm    
Thank you so much because of your guidance finally my file data is uploaded to the database may god bless you jazakALLAH.
Richard MacCutchan 13-Jun-22 2:58am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900