yiisoft / yii2-mongodb

Yii 2 MongoDB extension
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
326 stars 191 forks source link

Non-suppoted protocol `mongodb+srv` #304

Open ndobromirov opened 4 years ago

ndobromirov commented 4 years ago

https://github.com/yiisoft/yii2-mongodb/blob/837c14c8a38055cd97d6f8b551eef27616f3c8c5/src/Connection.php#L203

The recommended protocol for connection DSNs in some cloud providers is: mongodb+srv This is failing with an exception that a default database can not be found.

After setting the default database maually I am getting the following issue:

MongoDB\Driver\Exception\InvalidArgumentException: Failed to parse MongoDB URI: 'mongodb+srv://user:password@host/database'. 
Invalid URI Schema, expecting 'mongodb://'. in /var/www/html/web/vendor/yiisoft/yii2-mongodb/src/Connection.php:356

I am considering this a bug, as the official MongoDB connection library supports this and it's highly logical for this to be a thin wrapper around it. See PHP example here:

https://docs.atlas.mongodb.com/driver-connection/#driver-examples
LLabsAaron commented 4 years ago

Temporary workaround for anyone else experiencing this issue is to change it to: if (preg_match('/^mongodb(:|\+srv:)\\/\\/.+\\/([^?&]+)/s', $this->dsn, $matches)) {