I am new to MongoDB and just encountered two types of connection string.
-
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
-
mongodb+srv://[username:password@]host[/[database][?options]]
I know about the 1st one. But unfamiliar with the (+srv) in the 2nd.
let connectionUrl;
if (username && password)
connectionUrl = `mongodb://${username}:${password}@${host}:${
port || 27017
}/${databaseName}`;
else
connectionUrl = `mongodb://${host}:${
port || 27017
}/${databaseName}`;
console.log(connectionUrl, "connectionUrlconnectionUrl");
let connection = await mongoose.createConnection(connectionUrl, {
useNewUrlParser: true,
});
return connection;
Now the problem user can enter username, password, hostname, etc...
But is there any way to know when to add (+srv) because I was trying with localhost and with MongoDB atlas. Atlas works fine with +srv but in the case of localhost, it's throwing an error.


所有评论(0)