Mongoose and multiple database error handling

Viewed 199

I have 2 Databases, 1: in local and 2: in mlab, and with this Code I Can Check error for one of databases if my Connection has a Problem.

mongoose.connect('mongodb://localhost/test', { useMongoClient: true });
mongoose.Promise = global.Promise;

const db = mongoose.connection;

db.on('error', console.error);
db.once('open', () => {
    console.log(rangi.green('Connected To MongoDB'));
});

module.exports = db;


cron.schedule('1 * * * *', () => {
    const sync = mongoose.createConnection('mongodb://sync:sync@ds143542.mlab.com:');
    const remoteList = sync.model('User');
    remoteList.find({}, (err, docs) => {
        User.collection.insert(docs, status)
    })

    function status(err, result) {
        if (err && err.code == 11000) {
            console.log(rangi.red(`Err`));
        } else {
            console.info(rangi.magenta(`Sync Successful!`));
        }
    }
});

How Can I Check connection for My Second database?
How to add Multi-mongos support or Multiple connections and Error handling for That?

mongoose.connect('mongodb://mongosA:27501,mongosB:27501', { mongos: true }, cb);
1 Answers
Related