Hi all,
Having a strange issue, that I cannot figure out and need some help, please.
I’m developing a Client-Server application where the clients have access to MongoDB. While developing, the database is in the same machine as my client and server. This works absolutely fine.
Once I move the application to another client and run it, I can connect to MongoDB but getting all documents with get_documents
function, it hangs on the for loop of the cursor for doc in collection.find():
.
Here’s the script for MongoDB access:
import os
import json
import pymongo
from pymongo.errors import ConnectionFailure
CONFIG_PATH = os.path.join(os.getcwd(), "config.json")
class MongoDBConnection:
"""Main Class for the connecting to MongoDB"""
def __init__(self) -> None:
"""Initialization for the MongoDBConnection Class."""
with open(CONFIG_PATH) as file:
config_json = json.load(file)
db_connection = config_json["mongo_db"][0]
self.HOST = db_connection["host"]
self.PORT = db_connection["port"]
self.ADMIN_USERS = config_json["admin_users"]
self.db_connected = None
def connected(self) -> bool:
"""Confirm the connection with the database.
If there is a connection issue, ConnectionFailure is raised.
:return: The connection status
"""
try:
with pymongo.MongoClient(self.HOST, self.PORT) as client:
self.db_connected = client.ao_connect
return self.db_connected
except ConnectionFailure as err:
print(err)
return False
def get_documents(self, collection: pymongo.collection.Collection) -> list:
"""Get all of the documents contained in the collection.
:param collection: The collection Object selected
:return: Collection names
"""
docs_list = []
collection = self.db_connected[f"{collection.name}"]
for doc in collection.find():
docs_list.append(doc)
return docs_list
Here’s the error:
gbmud11368:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 603e5aa529766aed2c463091, topology_type: Single, servers: [<ServerDescription ('gbmud11368', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('gbmud11368:27017: timed out')>]>
Let me know if you need any other information.
Thank you very much in advance,
Andre