I\'m trying to connect to a MongoDB database with a username and password using Mongoose in Node.js. All the docs say that the connection string should look like
<
If you use Mongodb native Node.js driver, this is what works for me as of 3.1 driver version. Assume your url doesn't contain auth info.
MongoClient = require('mongodb').MongoClient;
let options = {
useNewUrlParser: true,
auth: {
user: 'your_usr',
password: 'your_pwd'
}
};
MongoClient.connect(url, options, callback);
Or if you wanna include auth info in your url, do this:
let url = "mongodb://username:" + encodeURIComponent("p@ssword") + "@localhost:27017/database"