{"record":{"id":"c97fbe3a5619e52b","repo":"mongodb/laravel-mongodb","slug":"mongodb-connection-configuration-requires-dsn-or-host-key","errorCode":null,"errorMessage":"MongoDB connection configuration requires \"dsn\" or \"host\" key.","messagePattern":"MongoDB connection configuration requires \"dsn\" or \"host\" key\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Connection.php","lineNumber":346,"sourceCode":"        $authDatabase = isset($config['options']) && ! empty($config['options']['database']) ? $config['options']['database'] : null;\n\n        return 'mongodb://' . implode(',', $hosts) . ($authDatabase ? '/' . $authDatabase : '');\n    }\n\n    /**\n     * Create a DSN string from a configuration.\n     */\n    protected function getDsn(array $config): string\n    {\n        if (! empty($config['dsn'])) {\n            return $this->getDsnString($config);\n        }\n\n        if (! empty($config['host'])) {\n            return $this->getHostDsn($config);\n        }\n\n        throw new InvalidArgumentException('MongoDB connection configuration requires \"dsn\" or \"host\" key.');\n    }\n\n    /** @inheritdoc */\n    #[Override]\n    public function getDriverName()\n    {\n        return 'mongodb';\n    }\n\n    /** @inheritdoc */\n    public function getDriverTitle()\n    {\n        return 'MongoDB';\n    }\n\n    /** @inheritdoc */\n    #[Override]\n    protected function getDefaultPostProcessor()","sourceCodeStart":328,"sourceCodeEnd":364,"githubUrl":"https://github.com/mongodb/laravel-mongodb/blob/0634653039468ceb0268a69192bdac64469ed043/src/Connection.php#L328-L364","documentation":"Connection::getDsn() builds the MongoDB DSN from config. It returns a DSN from the 'dsn' key if present, else builds one from 'host'. If neither key is provided (or the 'dsn' string is empty and 'host' is empty), it throws InvalidArgumentException.","triggerScenarios":"Defining a mongodb connection in config/database.php with only 'driver' => 'mongodb' and no 'dsn' or 'host' key; providing an empty 'host' array/string with no 'dsn'.","commonSituations":"New connection entry created from a copy that stripped both keys; config driven by env vars where both MONGODB_HOST and MONGODB_DSN are unset; misnamed key like 'hostname' instead of 'host'.","solutions":["Add a 'dsn' key to the connection config, e.g. 'dsn' => env('MONGODB_URI', 'mongodb://localhost:27017')","Or add a 'host' key, e.g. 'host' => ['localhost:27017']","Check env() defaults so local envs without variables still produce a usable host/DSN","Fix typos so the config key is exactly 'dsn' or 'host'"],"exampleFix":"// before\n'mongodb' => ['driver' => 'mongodb'],\n// after\n'mongodb' => ['driver' => 'mongodb', 'dsn' => env('MONGODB_URI', 'mongodb://localhost:27017'), 'database' => 'myapp'],","handlingStrategy":"validation","validationCode":"$config = config('database.connections.mongodb');\nif (empty($config['dsn']) && empty($config['host'])) {\n    throw new RuntimeException('mongodb connection needs dsn or host');\n}","typeGuard":"function hasDsnOrHost(array $config): bool {\n    return !empty($config['dsn']) || !empty($config['host']);\n}","tryCatchPattern":"try {\n    $conn = DB::connection('mongodb');\n} catch (InvalidArgumentException $e) {\n    if (str_contains($e->getMessage(), 'requires \"dsn\" or \"host\"')) {\n        Log::error('MongoDB connection config missing dsn/host');\n        // fix config, e.g. set MONGODB_URI env var\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Use env('MONGODB_URI', 'mongodb://localhost:27017') in the dsn key","Add a config validation step in CI that checks required connection keys","Copy the sample connection block from the library README verbatim and edit values only"],"tags":["configuration","mongodb","dsn"],"backgroundTag":"missing-required-config-field","analyzedSha":"0634653039468ceb0268a69192bdac64469ed043","analyzedAt":"2026-09-15T02:56:37.067Z","contentChangedAt":"2026-09-15T02:56:37.067Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}