MongoDB connection and setting database

Hello, I am having a hard time figuring out what is the problem. I installed mongo from Docker Hub via docker. When I try to connect it with this kind of connection URL it works perfectly:

Both from Compass and from my application I can connect to the database. However when I try to set the default database by adding a slash and the database name to the connection string it gives back an error: Authentication Failed.

So my URL with the database looks like this: mongodb://admin:password@host:port/db
Strange but when I try to connect to the admin database it lets me connect and sets that as the default database.

Can somebody help me how this is works? Thanks in advance.

(I created the database by hand, so that will not be the problem).

Hi @Zsolt_Gombocz

The documentation specifies the path component of the uri as the defaultauthdb.

So the option is doing what is says, changing the authentication source(database)

However by appending ?authSource=admin to the connection string it should work.

1 Like