I have a MongoDB on Azure and I am trying to connect to it using the npm module mongodb:
var mongoClient = require("mongodb").MongoClient;
mongoClient.connect("mongodb://myuser:[email protected]:10355/?ssl=true", function (err, db) {
db.close();
});
My password has the following characteristics:
=
, @
, $
and so onI get the following when executing the code above:
Error: Password contains an illegal unescaped character
at parseConnectionString (C:\Users\myuser\Documents\myproj\node_modules\mongodb\lib\url_parser.js:280:13)
However the documentation does not tell much about how to solve this issue. I guess it is an encoding problem. How to fix this?
Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.
The Azure Cosmos DB's API for MongoDB is compatible with MongoDB server version 3.6 by default for new accounts. The supported operators and any limitations or exceptions are listed below. Any client driver that understands these protocols should be able to connect to Azure Cosmos DB's API for MongoDB.
To connect to a MongoDB database, select Add Connection and enter the connection details for the database then Connect, the default is a local MongoDB server at mongodb://127.0.0.1:27017 . You can also enter a connection string, click the "connect with a connection string" link and paste the connection string.
Characters like @ are restricted as they mess up the structure of the URL. The reason for this is because MongoDB interprets it as the @ separator. Instead of this:
var mongoClient = require("mongodb").MongoClient;
mongoClient.connect("mongodb://myuser:myp@[email protected]:10355/?ssl=true", function (err, db) {
db.close();
});
use this
mongoClient.connect("mongodb://myuser:myp%[email protected]:10355/?ssl=true", {
uri_decode_auth: true
}, function (err, db) {
db.close();
});
To encode the 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();
});
On 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();
});
MongoDB can now use a password with special characters. To do this, add an option to the connection { useNewUrlParser: true }
:
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const uri = 'mongodb://mydbname:pa$s;w@[email protected]:27017/admin';
MongoClient.connect(uri, { useNewUrlParser: true }, (err, db) => {
assert.strictEqual(null, err);
// ...
db.close();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With