{"record":{"id":"635430d69c2c6b09","repo":"getredash/redash","slug":"mongodb-connection-error","errorCode":null,"errorMessage":"MongoDB connection error","messagePattern":"MongoDB connection error","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"redash/query_runner/mongodb.py","lineNumber":231,"sourceCode":"            kwargs[\"replicaSet\"] = self.configuration[\"replicaSetName\"]\n            readPreference = self.configuration.get(\"readPreference\")\n            if readPreference:\n                kwargs[\"readPreference\"] = readPreference\n\n        if self.configuration.get(\"username\"):\n            kwargs[\"username\"] = self.configuration[\"username\"]\n\n        if self.configuration.get(\"password\"):\n            kwargs[\"password\"] = self.configuration[\"password\"]\n\n        db_connection = pymongo.MongoClient(self.configuration[\"connectionString\"], **kwargs)\n\n        return db_connection[self.db_name]\n\n    def test_connection(self):\n        db = self._get_db()\n        if not db.command(\"connectionStatus\")[\"ok\"]:\n            raise Exception(\"MongoDB connection error\")\n\n        return db\n\n    def _merge_property_names(self, columns, document):\n        for property in document:\n            if property not in columns:\n                columns.append(property)\n\n    def _is_collection_a_view(self, db, collection_name):\n        if \"viewOn\" in db[collection_name].options():\n            return True\n        else:\n            return False\n\n    def _get_collection_fields(self, db, collection_name):\n        # Since MongoDB is a document based database and each document doesn't have\n        # to have the same fields as another documet in the collection its a bit hard to\n        # show these attributes as fields in the schema.","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/getredash/redash/blob/ca79fe988d81cdac9675b412f3dfcab107bc1fbc/redash/query_runner/mongodb.py#L213-L249","documentation":"Raised by MongoDBQueryRunner.test_connection() when the connectionStatus command on the connected database returns ok != 1. It indicates the server accepted a socket but the status command failed — typically auth/permission problems on the database rather than unreachable host.","triggerScenarios":"Clicking \"Test Connection\" on a MongoDB data source where credentials lack permission to run connectionStatus, the user is authenticated to the wrong database, or the server/HA-proxy returns an error status.","commonSituations":"Wrong username/password (auth source admin not specified in the connection string), MongoDB user created in a different auth database, or connecting to a mongos/Atlas tier where connectionStatus is disallowed for the role.","solutions":["Verify credentials and add the correct auth source, e.g. mongodb://user:pass@host/db?authSource=admin","Confirm the user has at least read access (and clusterMonitor for status commands) on the target database","Check server/replica-set health and that the URI points at the right deployment","If behind a proxy/Atlas, ensure the client IP is allow-listed"],"exampleFix":"// before\nmongodb://user:pass@host/mydb\n// after\nmongodb://user:pass@host/mydb?authSource=admin","handlingStrategy":"retry","validationCode":"# Pre-flight: parse the URI and confirm authSource present for credentialed users\nfrom urllib.parse import urlparse\nu = urlparse(uri)\nassert u.username is None or 'authSource' in (u.query or ''), 'add ?authSource=<db>'","typeGuard":null,"tryCatchPattern":"try:\n    runner.test_connection()\nexcept Exception as e:\n    if 'MongoDB connection error' in str(e):\n        check_credentials_and_authsource(); retry_once_after_fix()","preventionTips":["Always include authSource when users are defined in admin","Grant the connecting user read/clusterMonitor roles","Test connection after any credential or topology change"],"tags":["mongodb","redash","connection","authentication"],"backgroundTag":"database-connection-test-failed","analyzedSha":"ca79fe988d81cdac9675b412f3dfcab107bc1fbc","analyzedAt":"2026-08-28T18:32:34.637Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}