Client failing to return documents MongoDB

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

To test If it’s a remote access or permissions issue, I would use the mongo shell from the client and see if I can get a document outside of python.

( I’d also double check config.json and make sure it’s not pointed at the local host )

@Mambo4 Cheers for your help! :slightly_smiling_face:
Will have a look at using the mongo shell. The config doc is pointing to the server where the database lives, so it should in theory be configured correctly.

The firewall was blocking port 27017 :expressionless:.
Works absolutely fine now! :slight_smile:

Cheers guys!