Mongoose: How to switch database

Created on 24 Sep 2012  路  10Comments  路  Source: Automattic/mongoose

In the mongo shell, I can do like this:

use admin
db.auth('user','pass')
use test
db.find()

This means I pass the authurized then I can do everything.

My problem is how can I do the same work in mongoose, how can I switch db in mongonse?

Because I must enter the ADMIN db to authurize, and then chang to other db to my own manipulation

Most helpful comment

other possible way is with query param in url

 mongodb://root:pass@localhost:27017/mydb?authSource=admin

look here -> Support AuthSource

All 10 comments

to authorize your connection, include your uname + password in your connection string or in the params passed to connect:

see the examples here: http://mongoosejs.com/docs/api.html#index_Mongoose-createConnection

Thanks for reply!
Yes, I know how to authorize. But after authorization, how can I switch the database what "use OTHER_DB" in the mongo shell does.

It's not supported by mongoose. A new connection is required.

On Sep 24, 2012, at 8:57 PM, Rex Huang [email protected] wrote:

Thanks for reply!
Yes, I know how to authorize. But after authorization, how can I switch the database what "use OTHER_DB" in the mongo shell does.

聛\
Reply to this email directly or view it on GitHub.

Had problem to connect to a mongodb - coz the auth would bring me to a specific db like "admin" (standard). But how with mongoose? Only set the authSource inside of auth option....

var db = mongoose.createConnection(
  "localhost", 
  "myCostumDB", 
  "27010", 
  {
    user: "root",
    pass: "mypass",
    auth: {
      authSource: "admin" //<-- That is the Point.....
    }
  }
);

Good lord, I can't believe how hard it was to find this. Thanks @MNeundorf for saving me further hair pulling.

other possible way is with query param in url

 mongodb://root:pass@localhost:27017/mydb?authSource=admin

look here -> Support AuthSource

@Naxmeify you save my life...! thanks!

@Naxmeify you save my life too...! thanks!

@naxmefy You save also my life! Thanks!

how do i change user login portal to admin login portal on same page?

Was this page helpful?
0 / 5 - 0 ratings