简体   繁体   中英

NodeJS - MongoClient.Connect with URL to DB Other Than Default

I'd like to use the MongoClient.Connect(Url) option for connecting from Node and in fact it does work for authenticated connections to the admin database. However, if I try to specify a different database I get an "auth Fails" with code 18.

I think this means that the credentials are only setup on the admin database and, unfortunately, I can't change this arrangement (for now, anyway).

It seems this Url syntax is the direction the driver is heading, so I'd like to use it if possible. Is there a way to use this syntax, authenticate and connect to the admin database, and then get a connection to one of the other databases on the server?

I just found out how to do this: You use the instance you get ("admin") to open another database you have access to:

db2 = db.db("name of other name");

See it hidden inside the documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM