7

I have a nest.js node server and I am trying to connect mongoDB data base in the app.module, when the connection string doesn't contains the DB name - the connection to default DB "test" success, but when I specify the DB name- always getting "Authentication failed" error.

app.module.ts:

This works:

  imports: [
    MongooseModule.forRoot('mongodb://admin:admin@localhost:30000'),
  ]

But this specifying the DB name failed with Authentication error:

  imports: [
    MongooseModule.forRoot('mongodb://admin:admin@localhost:30000/test'),
  ]

or:

  imports: [
    MongooseModule.forRoot('mongodb://admin:admin@localhost:30000/data'),
  ]

Using MongoClient directly (without nestjs) connecting successfully:

const client = new MongoClient('mongodb://admin:admin@localhost:30000');
await client.connect();
db = client.db('data');

Any idea what is my problem and what should I do in order to solve this problem?

Thanks.

user2436448
  • 445
  • 4
  • 7
  • 18

7 Answers7

12

Specify the DB name as a connection option - not as a part of the connection string solved the problem:

imports: [
    MongooseModule.forRoot({
       uri: 'mongodb://admin:admin@localhost:30000',
       dbName: 'data'
    }),
  ]
user2436448
  • 445
  • 4
  • 7
  • 18
7

According to NestJS official document, forRoot() method accepts the same configuration object as mongoose.connect() from the Mongoose package.

You can see on the documentation of mongoose.connect(), all the options that you can apply.

You may define the db name this way:

imports: [
  MongooseModule.forRoot('mongodb://admin:admin@localhost:30000', {
    dbName: 'custom_db_name',
  })
]
mbelsky
  • 6,093
  • 2
  • 26
  • 34
Dickson PUN
  • 71
  • 1
  • 1
6

This actually is not supported on newest version of @nestjs/mongoose, for instance in version ^7.2.4 it receives an string as first parameter and an object as second parameter, so what worked for me was:

   imports: [
      MongooseModule.forRoot(
      'mongodb://user:password@localhost:27017/nestjs-tutorial?authSource=admin&readPreference=primary',
    ),
    customModule,
   ],
Erick Garcia
  • 510
  • 5
  • 9
2

As @Dickson pointed out, I tried to specify a database, but it didn't help until I checked the mongoose.connect() from the documentation. So here is the working code for me:

 imports: [
    MongooseModule.forRoot('mongodb://127.0.0.1:27017', {
      dbName: 'some_database',
    }),
  ],

As same as:

  MongooseModule.forRoot('mongodb://127.0.0.1:27017/some_database')

As the documentation says:

This is the minimum needed to connect the myapp database running locally on the default port (27017). If connecting fails on your machine, try using 127.0.0.1 instead of localhost.

What I needed was to rename localhost to the IP as @Ganesh tried to mention in his answer.

Giorgi Gvimradze
  • 1,714
  • 1
  • 17
  • 34
1

For some reason, My nest js app stopped working and the error was "can't connect to the database" After struggling for hours, I changed the localhost to 127.0.0.1 and everything started working.

Old code MongooseModule.forRoot('mongodb://localhost:27017/some_database')

New Code MongooseModule.forRoot('mongodb://127.0.0.1:27017/some_database')

I still don't understand why it happened and how 127.0.0.1 is different from localhost

Sanan Ali
  • 2,349
  • 1
  • 24
  • 34
0

I was facing the same issue after connecting MongooseModule in nest js until I changed my connection string mongodb://http://localhost:27017/task-prectice to mongodb://localhost:27017/task-prectice or you can replace localhost with 127.0.0.1

-1
MongooseModule.forRoot('mongodb://127.0.0.1:27017', { dbName: 'myapp' })
Giorgi Gvimradze
  • 1,714
  • 1
  • 17
  • 34