I have a mongodb in linux server I have created a user for specific database and user successfully created.
db.createUser(
{
user: “user1",
pwd: “user123",
roles: [ { role: "readWrite", db: “Products" }]
}
)
When I try to connect with credential and database in mongo compass showing invalid credential but when I go without database name only credential working fine and I'm able to access database as well.
I want to connect credential and database in mongo compass and want to connect in node js application as well My nodejs connection code is
const url = 'mongodb://user1:user123@IP:27017/?authMechanism=DEFAULT';
mongoose.connect(url,{ dbName: 'Products' }, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true })
const con = mongoose.connection
con.on('open', () => {
console.log('connected...')
})
Node js code throwing error as
MongoNetworkError: failed to connect to server [IP:27017] on first connect [MongoNetworkTimeoutError: connection timed out