我有2个数据库,1:在local
和2:在mlab
中,如果我的连接有问题,我可以检查其中一个数据库的错误。
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!`));
}
}
});
如何检查My Second数据库的连接?
如何添加Multi-mongos support
或Multiple connections
以及错误处理?
mongoose.connect('mongodb://mongosA:27501,mongosB:27501', { mongos: true }, cb);
答案 0 :(得分:2)
您可能正在寻找以下内容:
var localConnectStr = 'mongodb://localhost/test'
var mlabConnectStr = 'mongodb://<dbuser>:<dbpassword>@ds123456.mlab.com:25716/<dbname>'
var db = mongoose.createConnection(localConnectStr, { useMongoClient: true });
var mlabdb = mongoose.createConnection(mlabConnectStr, { useMongoClient: true });
您当然希望使用您的实际mlab uri和数据库用户/ pass。但这就是你如何处理多个连接。 See createConnection here.
你可以使用你拥有的代码,检查你的本地连接,检查mlab。
mlabdb.on('error', console.error);
mlabdb.once('open', () => {
console.log(rangi.green('Connected To Mlab MongoDB'));
});