{"record":{"id":"9823e762b4362c05","repo":"mongodb/laravel-mongodb","slug":"database-is-not-properly-configured","errorCode":null,"errorMessage":"Database is not properly configured.","messagePattern":"Database is not properly configured\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Connection.php","lineNumber":225,"sourceCode":"            // The parent method enable query log using enableQueryLog()\n            // but disables it by setting $loggingQueries to false. We need to\n            // remove the subscriber for performance.\n            if (! $this->loggingQueries) {\n                $this->disableQueryLog();\n            }\n        }\n    }\n\n    /**\n     * Get the name of the default database based on db config or try to detect it from dsn.\n     *\n     * @throws InvalidArgumentException\n     */\n    protected function getDefaultDatabaseName(string $dsn, array $config): string\n    {\n        if (empty($config['database'])) {\n            if (! preg_match('/^mongodb(?:[+]srv)?:\\\\/\\\\/.+?\\\\/([^?&]+)/s', $dsn, $matches)) {\n                throw new InvalidArgumentException('Database is not properly configured.');\n            }\n\n            $config['database'] = $matches[1];\n        }\n\n        return $config['database'];\n    }\n\n    /**\n     * Create a new MongoDB connection.\n     */\n    protected function createConnection(string $dsn, array $config, array $options): Client\n    {\n        // By default driver options is an empty array.\n        $driverOptions = [];\n\n        if (isset($config['driver_options']) && is_array($config['driver_options'])) {\n            $driverOptions = $config['driver_options'];","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/mongodb/laravel-mongodb/blob/0634653039468ceb0268a69192bdac64469ed043/src/Connection.php#L207-L243","documentation":"The connection needs a database name, resolved either from the 'database' config key or parsed out of the DSN path. If the config has no 'database' and the DSN does not contain a /dbname path segment, Connection::getDefaultDatabaseName() throws InvalidArgumentException.","triggerScenarios":"Creating a MongoDB connection (db connection in config/database.php) where 'database' is empty/null and the 'dsn' has no database in its path, e.g. 'mongodb://localhost:27017' with no trailing '/mydb'.","commonSituations":"Config copied from examples that authenticate via DSN options but omit the db path; empty env var (MONGODB_DATABASE='') silently producing an empty database config; switching from host/port config to DSN without adding the db segment.","solutions":["Add a database path to the DSN: 'mongodb://localhost:27017/mydb'","Set the 'database' key in the connection config in config/database.php","Verify the MONGODB_DATABASE env var is not empty if the config reads from env()","Check the DSN for typos — the database must appear after the host before any '?'"],"exampleFix":"// before\n'dsn' => 'mongodb://localhost:27017?retryWrites=true',\n// after\n'dsn' => 'mongodb://localhost:27017/myapp?retryWrites=true',","handlingStrategy":"validation","validationCode":"$config = config('database.connections.mongodb');\nif (empty($config['database']) && !preg_match('/^mongodb(?:\\+srv)?:\\/\\/.+?\\/([^?&]+)/s', $config['dsn'] ?? '', $m)) {\n    throw new RuntimeException('Set database key or add /dbname to the DSN');\n}","typeGuard":"function hasResolvableDatabase(array $config): bool {\n    return !empty($config['database'])\n        || (is_string($config['dsn'] ?? null) && preg_match('/^mongodb(?:\\+srv)?:\\/\\/.+?\\/([^?&]+)/s', $config['dsn'], $m) === 1);\n}","tryCatchPattern":"try {\n    DB::connection('mongodb')->table('users')->get();\n} catch (InvalidArgumentException $e) {\n    if (str_contains($e->getMessage(), 'not properly configured')) {\n        config(['database.connections.mongodb.database' => env('MONGODB_DATABASE', 'default_db')]);\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Always set 'database' explicitly in config/database.php","Give env('MONGODB_DATABASE', 'fallback') a default in config","Include the database path in the DSN URI for production environments"],"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"}