0

Here is my code in my NodeJS application, to connect to my MongoDB engine :

 const collection = 'mynewcollection';
 const password = 'passwordwithan@';
 const mongoUrl = `mongodb://admin:${encodeURIComponent(password)}@mymongobase.net/${collection}`;

 // Connect using the connection string
 MongoClient.connect(mongoUrl, {useNewUrlParser: true}, function(err, db) {
   console.log(err.toString())
 });

I get an authentication error. I tried several things to handle the '@' character and reading the documentation I thought that it was the good one... But it is still failing even if the user and password are the good one.

Is the API correctly used ? Do you understand what is wrong ?

Thanks in advance.

  • 1
    Possible duplicate of [MongoDB password with "@" in it](https://stackoverflow.com/questions/7486623/mongodb-password-with-in-it) – Theo Aug 08 '19 at 14:36
  • After more investigation, the @ is not a problem. In fact, Mongo does not find my super user "admin" when I try to connect with it... If I create a new user and try to connect with it, it's OK. I don't understand why the super user (which seems to be common to every database of my engine) is not recognized when I try to login. – Romain COMTET Aug 08 '19 at 15:03

1 Answers1

1

OK I found the solution. If you use :

const mongoUrl = `mongodb://admin:${encodeURIComponent(password)}@mymongobase.net/${collection}`;

Mongo will try to connect with admin/password defined on the collection.

If you don't put the collection in the mongo url :

const mongoUrl = `mongodb://admin:${encodeURIComponent(password)}@mymongobase.net`;

Then you can use the super user defined on all the mongo engine.