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 accessing documents is an issue, as with 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.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
Wondering if this can be an issue with permissions when accessing remotely? I can’t really find an answer.
Thank you in advance,
Andre