MongoDB password with "@" in it

iZ. Source

I'm trying to connect to a MongoDB database with a username and password using Mongoose in Node.js. All the docs say that the connection string should look like

  mongodb://username:[email protected]:port/db

However, the password contains the '@' character in it. How can I make a connection string out of this that mongoose will understand? Can I escape the '@' in the password or is there another method of connecting I have to use?



answered 6 years ago JohnnyHK #1

Use the options parameter of the mongoose.connect call to specify the password instead of including it in the URL string:

                 {user: 'username', pass: '[email protected]'},

answered 4 years ago Andrey Hohutkin #2

Use this syntax, don't forget the uri_decode_auth option.

mongoClient.connect("mongodb://username:p%[email protected]:port/dbname", { 
    uri_decode_auth: true 
    }, function(err, db) {


answered 2 years ago Viet Tran #3

Try this one, my friends:

                     {user: 'viettd', pass: '[email protected]'});

test is my db name
admin is my the db for authenticating
viettd is my username
[email protected] is my password

answered 2 years ago AKASH #4

use pwd instead pass, that worked for me for version3.2

                 {user: 'username', pwd: '[email protected]'},

answered 2 years ago dang #5

sometimes you need to connect to the DB using other tools that accept string only as connection string. so just change the @ sign with %40

answered 7 months ago vanduc1102 #6

If your password has special characters:

const dbUrl = `mongodb://adminUsername:${encodeURIComponent('adminPassword')}@localhost:27017/mydb`;

answered 7 months ago Oduwole Oluwasegun #7

Also, if your password contains a percentage, %, 
 Because the percent ("%") character serves as the indicator for percent-encoded octets, it must be percent-encoded as "%25" for that octet to be used as data within a URI

for example, if your password is John%Doe, the new transformed password will be John%25Doe or
If password is Paul%20Wait, New Password will be Paul%2520Wait

mongoClient.connect("mongodb://username:John%[email protected]:port/dbname", function(err, db) {
// password is John%Doe
    }`enter code here`

answered 5 months ago prisan #8

I have also faced the same issue. I have solved by adding encoded password into connection string. And it works just well.

(1) Encode your password from (2) Replace your password with encoded one. (3) It should work well.

For ex: Actual Password: ABCDEX$KrrpvDzRTy`@drf.';3X Encoded Password: ABCDEX%24KrrpvDzRTy%60%40drf.%27%3B3X

mongodb://user1:ABCDEX%24KprpvDzRTy%60%40drf.%27%[email protected]:1234,',

answered 1 month ago Thierry #9

None of the solutions mentioned above worked for me. I researched it further and found out that I had to include the useNewUrlParser parameter.

mongoose.connect(db, {
    useNewUrlParser : true
    err => {
    if (err){
        console.error('Error: ' + err)
        console.log('Connected to MongoDb')

From what I understand, you need a specific version of MongoDB in order to use this. For more details, check Avoid “current URL string parser is deprecated” warning by setting useNewUrlParser to true

It is to get rid of the warning but clearly the version also affect the required parameter.

I haven't tested all special characters but it definitely works with '@#$'.

Hope this helps.

comments powered by Disqus