Question

Is there a way to instanciate a MongoStore to a db and auth with the "admin" one ?

Like this with mongoose:

var db = mongoose.createConnection('mongodb://myname:mypwd@localhost:27017/mydb', { auth: { authdb:"admin" } });
Was it helpful?

Solution

When you talk about MongoStore do you mean this project - https://github.com/diversario/connect-mongostore ?

If you do then the answer is yes, it uses the same syntax as URI part of mydb example. You just need to change the values in the user and password fields to the appropriate for your database. In the snippet below, it is connecting to the mongod instance running on port 27101 with a username of 'user' and a password of 'password' to the 'admin' database.

new MongoStore('mongodb://user:password@127.0.0.1:27017/admin')

OTHER TIPS

Best way to do it is -

'mongodb://' + mongo_username + ':' + encodeURIComponent(mongo_password) + '@' 
+ ip + ':27017/' + dbName+ '?authSource=admin'

encodeURIComponent() is used for, if password contains special chars

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top