1

We are in the process of updating our API's MongoDB hosting provider from mLab to MongoDB Atlas.

I have updated our connection server to PHP 7.4 with MongoDB PHP extension 1.7.4.

I have updated our API framework from Apigility to Laminas API Tools using the DoctrineMongoODMModule

I can successfully connect using the mongo shell using the following syntax:

mongo "mongodb+srv://test-server-dbteb.mongodb.net/<dbname>" --username <username>

I have looked far and wide to find a sample configuration of the DoctrineMongoODMModule with it's configuration file to connect to a MongoDB Atlas replica set using the mongo+srv:// protocol with no success to this point. Currently the errors are Failed to parse MongoDB URI.

If anyone has had a similar experience, any help would be greatly appreciated.

Jamie_D
  • 979
  • 6
  • 13

1 Answers1

1

I had the same issue and im still looking forward to get a real solution.

I directly edited the ConnectionFactory.php in the doctrine-mongo-odm-module (src/DoctrineMongoODMModule/Service/ConnectionFactory.php) with the following code

//...
  if (empty($connectionString)) {
        $connectionString = 'mongodb+srv://';  //prev: 'mongodb://'

        $user     = $options->getUser();
        $password = $options->getPassword();
        $dbName   = $options->getDbName();

        if ($user && $password) {
            $connectionString .= $user . ':' . $password . '@';
        }

        $connectionString .= $options->getServer() /*. ':' . $options->getPort()*/;

        if ($dbName) {
            $connectionString .= '/' . $dbName;
        }
    } else {
        // parse dbName from the connectionString
        $dbStart = strpos($connectionString, '/', 11);
        if ($dbStart !== false) {
            $dbEnd  = strpos($connectionString, '?');
            $dbName = substr(
                $connectionString,
                $dbStart + 1,
                $dbEnd ? ($dbEnd - $dbStart - 1) : PHP_INT_MAX
            );
        }
    }
//...

and in my connection file:

//...
'odm_default' => array(
            'server'           => '<host>',
            'port'             => '',
            'connectionString' =>  null,
            'user'             => '<user>',
            'password'         => '<psw>',
            'dbname'           => '<db>',
            'options'          => array()
        ),
//...

I think this is the worst possible solution (editing vendor' files) but in my case worked, take that as a temporary fix

sfeb
  • 11
  • 1
  • 1