mongodbnestjsmongo-shell

How to connect directly to a database with mongosh


I'm having connection problems with my local mongodb instance. Its not that it is not working, but there are some things that don't work, which I expected to work.

In my case I'm trying to setup my NestJS app with mongo (this tutorial). It states at some point:

MongooseModule.forRoot('mongodb://localhost/blog')

In my case it would be

MongooseModule.forRoot('mongodb://admin:admin@0.0.0.0:27017/blog')

Now whatever I do, it will not connect as long as I have the database name in that string.

I can reproduce this problem with mongosh too

$> mongosh mongodb://admin:admin@0.0.0.0:27017/blog
Current Mongosh Log ID: 640c5eeaaf2a65c11a7dda25
Connecting to:      mongodb://<credentials>@0.0.0.0:27017/blog? directConnection=true&appName=mongosh+1.8.0
MongoServerError: Authentication failed.

But when I remove the database name it works (also in my NestJs app). I'm not very familiar with Mongo, so maybe I'm missing the point completely, but I think this should work and you have to connect to a database before you can do anything. So my question is, what am I doing wrong here? Any help would be apprecited!


Solution

  • You can use connection string like this :

    mongosh "mongodb://user:pwd@localhost:27017/mybase" --authenticationDatabase admin

    This will connect directly to your database (mybase) , and your user, pwd will be checked on admin db