问题
I am creating a simple database CRUD manager using python and connecting it to a MongoDB database. I have connected to my cluster and have retrieved posts before using the exact same method. However this time when I tried, it does not work.
The error is when I try to print the dictionary object from a collection. The collection only contains a single object with {title:"book"} In the following code, I have retrieved a pymongo.cursor object as the variable name "thedb".
print(thedb)
will return
<pymongo.cursor.Cursor object at 0x01175D10>
It is possible that it returns an empty dictionary but I do not know anyway of testing if it is empty.
- I have already tried setting the SSL=True and the certificate.
client = pymongo.MongoClient(host,ssl=True,ssl_cert_reqs=ssl.CERT_NONE)
This has produced no difference to the result.
- I have also tried:
thedb = posts.find()
to return all objects and it gives the same error.
- I have also tried:
for a in thedb:
print(a["title"])
It should be a dictionary so this would return the value "book".
- I have also tried creating a new database and collection in MongoDB to connect.
import pymongo
import mongoengine
username = "username"
password = "password"
host = "mongodb+srv://"+username+":"+password+"@nameofmycluster-bfdug.mongodb.net/test?retryWrites=true&w=majority"
client = pymongo.MongoClient(host)
db = client.pymongo_test
# "pymongo_test" is the name of my database
mongoengine.connect('mongoengine_test', host=host)
posts = db.moreposts
# "moreposts" is the name of the collection in pymongo_test
thedb = posts.find({'title':"book"})
for a in thedb:
print(a)
Traceback (most recent call last):
File "C:/Users/J/Documents/Projects/Python/jobapplications/connectdb.py", line 128, in <module>
main()
File "C:/Users/J/Documents/Projects/Python/jobapplications/connectdb.py", line 102, in main
for a in thedb:
File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymongo\cursor.py", line 1225, in next
if len(self.__data) or self._refresh():
File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymongo\cursor.py", line 1117, in _refresh
self.__session = self.__collection.database.client._ensure_session()
File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymongo\mongo_client.py", line 1598, in _ensure_session
return self.__start_session(True, causal_consistency=False)
File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymongo\mongo_client.py", line 1551, in __start_session
server_session = self._get_server_session()
File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymongo\mongo_client.py", line 1584, in _get_server_session
return self._topology.get_server_session()
File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymongo\topology.py", line 434, in get_server_session
None)
File "C:\Users\J\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymongo\topology.py", line 200, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed
Process finished with exit code 1
回答1:
I couldn't find a solution so what I tried was to create a new MongoDB project with a new cluster. The original project was using AWS as the cloud provider and region for the cluster. This time I used Azure as the cloud provider and region for the new cluster in the new project.
It is working now and so the problem was lying within the connection to the database. All I did was to set up a new database so actually, the original problem still stands in the old database.
回答2:
pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed
Every cluster has 3 replica set nodes. It looks like you couldn't connect to any of them. On your Atlas web interface go to your Security > Network access settings
and make sure your IP address is whitelisted there.
来源:https://stackoverflow.com/questions/57343409/how-do-i-retrieve-and-print-pymongo-cursor-cursor-objects