Skip to content

Commit e1e95a5

Browse files
committed
get database from DSN string
1 parent 459d7e8 commit e1e95a5

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Jenssegers/Mongodb/Connection.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(array $config)
4141
$this->connection = $this->createConnection($dsn, $config, $options);
4242

4343
// Select database
44-
$this->db = $this->connection->selectDatabase($config['database']);
44+
$this->db = $this->connection->selectDatabase($this->getDatabaseDsn($dsn));
4545

4646
$this->useDefaultPostProcessor();
4747

@@ -191,10 +191,19 @@ protected function getHostDsn(array $config)
191191

192192
// Check if we want to authenticate against a specific database.
193193
$auth_database = isset($config['options']) && !empty($config['options']['database']) ? $config['options']['database'] : null;
194-
195194
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
196195
}
197196

197+
/**
198+
* Get database name from DSN string.
199+
* @param string $dsn
200+
* @return string
201+
*/
202+
protected function getDatabaseDsn($dsn)
203+
{
204+
return trim(parse_url($dsn, PHP_URL_PATH), '/');
205+
}
206+
198207
/**
199208
* Create a DSN string from a configuration.
200209
*

0 commit comments

Comments
 (0)