Characters like @ are limited because they ruined the structure of the URL. The reason for this is because MongoDB interprets this as an @ delimiter. Instead of this:
var mongoClient = require("mongodb").MongoClient; mongoClient.connect("mongodb://myuser: myp@ssword @myhost.documents.azure.com:10355/?ssl=true", function (err, db) { db.close(); });
use this
mongoClient.connect("mongodb://myuser:myp% 40ssword@myhost.documents.azure.com :10355/?ssl=true", { uri_decode_auth: true }, function (err, db) { db.close(); });
To encode a password, use encodeURIComponent(password)
You can also use this syntax.
mongoClient.connect("mongodb://myhost.documents.azure.com:10355/?ssl=true", {user: 'username', pass: ' p@ssword '}, function (err, db) { db.close(); });
In later versions use
auth: { user: 'username', password: ' p@ssword ', }
as below
mongoClient.connect("mongodb://myhost.documents.azure.com:10355/?ssl=true", { auth: { user: 'username', password: ' p@ssword ', }}, function (err, db) { db.close(); });
source share