These are chat archives for Automattic/mongoose

23rd
Nov 2018
NomNomSu
@NomNomSu
Nov 23 2018 11:03

Hello, I have 2 servers running expressjs and mongodb with 2 databases "database1" and database2" expressjs server has such config:

const express = require('express'),
    path = require('path'),
    bodyParser = require('body-parser'),
    mongoose = require('mongoose'),

computerRoutes = require('./routes/computer.route'); // importing defined routes

    mongoose.Promise = global.Promise;
    mongoose.connect('mongodb://172.28.0.1/database1').then( // ,< how to make it switch database for different hosts or connect to two databases at the same time
      () => {console.log('Database is connected') },
      err => { console.log('Can not connect to the database'+ err)}
    );

const app = express();
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false })); //added for cross
    app.use('/express', computerRoutes);

const port = process.env.PORT || 4000;

const server = app.listen(port, function(){
    console.log('Listening on port ' + port);
});

Next I have 2 hosts lets say 172.28.0.2 and 172.28.0.3. For IP 0.2 mongoose should connect to database1 and for IP 0.3 mongoose should connect to database2. How to do so? Or even better how to make mongoose connect to two databases at the same time ?