2

I am trying to figure out how to connect to my mongodb db using the native node mongo driver and I have two issues:

  • My password contains an @ sign making it break the normal user:pass@host connection string format
  • How do I list databases from what I have below?

Any ideas on how to address this?

Here is an attempt which does not work:

var Mongo = require('mongodb');

var server = new Mongo.Server('mongodb://myhost', 27017);
var db = new Mongo.Db('test', server);

db.open(function(err, db) {
    console.log(err); //unable to connect
});
doremi
  • 14,921
  • 30
  • 93
  • 148

2 Answers2

3

For future readers, I was able to resolve this with the connection option uri_decode_auth. You will need to encodeURIComponent(password) before embedding it in the connection string.

Here's a complete working example:

MongoClient.connect(connection, { uri_decode_auth: true }, function(err, db) {

  if(err) {
    return cb(err);
  }

  db.admin().listDatabases(function(err, dbs) {
    console.log(dbs);
  });
});
doremi
  • 14,921
  • 30
  • 93
  • 148
2

As mentioned on this answer:

The solution is to replace @ with %40

I tested with the C# driver and it works like a charm.

Community
  • 1
  • 1
Custodio
  • 8,594
  • 15
  • 80
  • 115