0

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.

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

  1. I have also tried:
thedb = posts.find()

to return all objects and it gives the same error.

  1. I have also tried:
for a in thedb:
    print(a["title"])

It should be a dictionary so this would return the value "book".

  1. 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
Jdevit
  • 1
  • 1
  • 2
  • 1
    Yes it is possible to get an empty cursor. The easiest way is to convert your cursor to a list, then you have all the `list` capabilities and avoid the destructive read of the cursor. Try - `thedb = list(posts.find())` – DaveStSomeWhere Aug 04 '19 at 04:30
  • Thank you for the help Dave. It doesn't solve the issue but I appreciate learning more. – Jdevit Aug 04 '19 at 13:57

3 Answers3

1

just started using mongoDB this morning and came across the same problem. I was looking for a specific value, found it, but got the same result as you. I just put "[0]" at the and got the result. As I said, I just started using mongoDB thats why I don't know why that is.

This got me my result: db.find({"key":"value"})[0]

Guenyo
  • 11
  • 1
  • 4
0

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.

Jdevit
  • 1
  • 1
  • 2
0
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.