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.
[Nest] 53087 - 11/08/2023, 6:30:46 PM ERROR [MongooseModule] Unable to connect to the database. Retrying (1)...
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');
What is the issue?
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'
}),
]