MongoDB password with "@" in it

Solution 1:

Use this syntax:

// use %40 for @
mongoClient.connect("mongodb://username:p%40ssword@host:port/dbname?authSource=admin", { 
        useNewUrlParser: true
    }, function(err, db) {

    }
);

Solution 2:

If your password has special characters:

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

Solution 3:

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

mongoose.connect('mongodb://localhost/test',
                 {user: 'username', pass: 'p@ssword'},
                 callback);

Solution 4:

Try this one, my friends:

    mongoose.connect("mongodb://localhost:27017/test?authSource=admin",
                     {user: 'viettd', pass: 'abc@123'});

test is my db name
admin is my the db for authenticating
viettd is my username
abc@123 is my password