10

I create mongoose.js model:

db = mongoose.createConnection(mongoUrl)

model = db.model('Model', schema)

can I get mongoUrl (or server and DB name) from model?

2

1 Answer 1

21

Yeah, you can reference the original DB connection through model.db. So you can get all the connection information you need like this:

console.log(model.db.host); // localhost
console.log(model.db.port); // 27017
console.log(model.db.name); // myDatabase
Sign up to request clarification or add additional context in comments.

2 Comments

cool, that worked for me. Is there a way to get the connection url information, without pointing to a specific model, but getting it from require(''mongoose") somehow?
@AlexanderMills, I know this is a bit late, but yes, mongoose.connection will be the same as model.db as long as your model is using the default connection, which it most probably is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.