gaurrus
gaurrus

Reputation: 136

How to handle two different connections to MongoDB

I'm working currently with one NodeJS + Express + Mongodb Applicacion and I need to get information stored in another monogodb database. So in my app.js file I'm doing the following steps in my app: app.js

var session         = require('express-session');
var MongoStore      = require('connect-mongo')(session);
var mongoose        = require('mongoose');
var passport        = require('passport');

var configDB        = require('./config/database.js'); 
// here I get the info from the Database IP and port.
mongoose.connect(configDB.url);
app.use( session({store: new MongoStore({mongoose_connection:                mongoose.connections[0]}),
    secret: 'secretPass',
    cookie: {maxAge: 36000}, // session secret
    saveUninitialized: true,
    resave: true}
));

Is there a way to add another connection so I can use it so get information stored in the second database?

Thanks a Lot!

Upvotes: 0

Views: 335

Answers (1)

user3078643
user3078643

Reputation: 129

You can use the following code:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/database1'); // database connection
mongoose.connect('mongodb://localhost/database2');

Upvotes: 2

Related Questions