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
<
use pwd instead pass, that worked for me for version3.2
mongoose.connect('mongodb://localhost/test',
{user: 'username', pwd: 'p@ssword'},
callback);
Use this syntax:
mongoClient.connect("mongodb://username:p%40ssword@host:port/dbname?authSource=admin", {
useNewUrlParser: true
}, function(err, db) {
}
);
This one worked for me
This one is a MongoDB 2020 Update If You're using a separate env file then just add your
mongoose.connect('url',
{
useNewUrlParser: true,
useUnifiedTopology: true
});
I was attempting this in python and I had a similar error. This worked for me.
import pymongo
client = pymongo.MongoClient("mongodb://username:12%40password@ip:27017/sample_db")
db = client.sample_db
# print the number of documents in a collection
print(db.collection.count())
12%40password represents your password and assumes it has a special character (e.g. @ - represented by %40) - username is your mongodb username , ip - your ip address and sample_db the database under mongodb that you wish to connect to.