Frage

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" } });
War es hilfreich?

Lösung

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')

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top