Mongodb connection URI

Hi,

I am trying to connect with MongoDB with the node
and access control is also enabled
using URI
mongodb://USER:PASSWORD@HOSTANME:27017/DATABASE_NAME
but it is not connecting to the database but when I add ?authSource=authDB at the end of the URI it is connected successfully

is there any way to connect with MongoDB without specifying authSource at the end of the URI?

Conditionally, the answer is “yes”, depending on your setup. See the authSource Connnection Option documentation.

2 Likes

hi,

I have MongoDB on my system and I want to connect with the node
using bellow URI

mongodb://shakir_usr1:PASSWORD@Hostname:27017/shakir_db?authSource=admin

when I add ?authSource=admin after the db name it will connect.
if I remove ?authSource=admin at the end it will not connect.

My USER is created in admin db.

when adding?authSource=admin in the last which worked.

but I want it without ?authSource=admin it should work as well get this error in error log
“error”: “UserNotFound: Could not find user "shakir_usr1" for db "shakir_db"”}

If you read the information in the link I posted, you will have your answer.

Ok,

I have checked the link you provided.

that means I have to define authSource if I add a database name in the uri.

Thanks

What it says is this:

If authSource is unspecified, authSourcedefaults to the defaultauthdb specified in the connection string.

If defaultauthdb is unspecified, then authSource defaults to admin.

1 Like

You can connect to MongoDB without specifying authSource in the URI if the authentication database is the same as your target database. Otherwise, include it for clarity and security.