Search code examples
node.jsmongodbmongoosenosql

String connection for mongoDB using mongoose with nodeJS


Do you why i can't log in with a connetion string like this: mongodb://root:*****@localhost:27017/Database But, i can log in with a one like this: mongodb://root:*****@localhost:27017/ I had an "authentication failed"

Also, i can log in like that: mongodb://root:*****@localhost:27017/admin

I don't know where i can looking for a solution.

Thank you for your help

I've tried with a database with a name in lowercase but nothing works.


Solution

  • MongoDB uses admin DB for authentication and authorization purposes.

    From official MongoDB document on Connection URI,

    If specified, the client will attempt to authenticate the user to the authSource. If authSource is unspecified, the client will attempt to authenticate the user to the defaultauthdb. And if the defaultauthdb is unspecified, to the admin database.

    As you are providing username:password@, you are implying the usage of defaultauthdb.

    • mongodb://root:*****@localhost:27017/admin works because you specified admin as the defaultauthdb
    • mongodb://root:*****@localhost:27017/ works because defaultauthdb is unspecified. So it is referred to the admin database

    mongodb://root:*****@localhost:27017/Database does not work because you are using database other than admin db to perform the auth.