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" } });
有帮助吗?

解决方案

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

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top