MongoClient, connect with '@' in password

php-mongodb

Solution

https://www.php.net/manual/en/mongoclient.construct.php you can pass the username and password as an options array

`new MongoClient("mongodb://localhost:27017", array("username" => "joe", "password" => "test"));`

Problem

I've been trying to experiment with MongoDb, and when I run the script to connect, it can't because the password uses the `@` symbol. To connect to MongoDb `mongodb://username:password@localhost` But as my password to connect to MongoDb uses the `@` symbol, it can't connect due to the `@` symbol. For example; ` new MongoClient("mongodb://mongo:p@assword@localhost);` Will throw the following error; `PHP Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: assword!@localhost:27017: Couldn't get host info for assword!@localhost'` As you can see, it thinks the first `@` in the password is the seperator for password and host. Is there a way to connect, and allow the `@` symbol in the password when using MongoClient?

Original source