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

房东的猫 提交于 2021-02-08 11:32:31

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/23201683/nodejs-mongoclient-connect-with-url-to-db-other-than-default

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!