Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to switch database #1114

Closed
seasonhuang opened this issue Sep 24, 2012 · 10 comments
Closed

How to switch database #1114

seasonhuang opened this issue Sep 24, 2012 · 10 comments

Comments

@seasonhuang
Copy link

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

@aheckmann
Copy link
Collaborator

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

@seasonhuang
Copy link
Author

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.

@aheckmann
Copy link
Collaborator

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

On Sep 24, 2012, at 8:57 PM, Rex Huang notifications@github.com 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.

@ghost
Copy link

ghost commented Jun 21, 2013

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.....
    }
  }
);

@buchanae
Copy link

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

@naxmefy
Copy link

naxmefy commented Aug 29, 2014

other possible way is with query param in url

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

look here -> Support AuthSource

@scarletsky
Copy link

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

@zjgnlzq
Copy link

zjgnlzq commented Nov 29, 2015

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

@EightSQ
Copy link

EightSQ commented Aug 8, 2016

@naxmefy You save also my life! Thanks!

@sudeeprathod
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants