Question

I read that in a nodejs application, if we use a mongoose object using

var obj = require('mongoose');

then, wherever we create a new mongoose object (say, in another file), then the same cached copy is used throughout. In that case, how can we connect to multiple database instances of mongoDB in the same nodejs app ?

Was it helpful?

Solution

You can use mongoose.createConnection to connect to a separate database, and then using the connection object that generates, you can define models from that database using connection.model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top