diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 733d2bbdf..3c98ec677 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -111,20 +111,22 @@ protected function getDsn(array $config) // Treat host option as array of hosts $hosts = is_array($config['host']) ? $config['host'] : array($config['host']); - foreach ($hosts as &$host) + $credentials = null; + + if (isset($config['username']) and isset($config['password'])) { - if (isset($config['username']) and isset($config['password'])) - { - $host = "{$username}:{$password}@{$host}"; - } + $credentials = "{$username}:{$password}@"; + } + foreach ($hosts as &$host) + { if (isset($config['port'])) { $host = "{$host}:{$port}"; } } - return "mongodb://" . implode(',', $hosts) . "/{$database}"; + return "mongodb://" . $credentials . implode(',', $hosts) . "/{$database}"; } /**