多个mongoDB数据源,怎么配置动态切换?

2024-11-16 16:29:49
推荐回答(1个)
回答1:

1:配置多个数据连接,如下
dbUrl = 'mongodb://'+configs.dbconfig.dbmongodbhealth.host+':'
+ configs.dbconfig.dbmongodbhealth.port.toString() +'/'+configs.dbconfig.dbmongodbhealth.dbname ;
var healthdbtoptions = {db: {},
server: { poolSize: 5 , auto_reconnect:true } ,
replSet: {},
mongos: {}

} ;
MongoClient.connect(dbUrl,healthdbtoptions ,function(err, database) {
if(err) throw err;
dbs.healthdb = database ;
console.info(dbUrl + " open ok healthdb");
//
});
2:调用数据访问的时候动态传递要使用的链接
var publicdb = opts.dbs.publicdb ;
--------------
exports.getAreaByPcodeAsync = function(opts){
var results = {error_code:-1,error_msg:"error"} ;
var bbPromise = opts.dbs.bbpromise ;
var collectionName = "D10002";
var publicdb = opts.dbs.publicdb ;
var filter = {pcode:opts.area.pcode}
return publicdb.collection(collectionName).find(filter).toArrayAsync().
then(function(areas){
var selectArray = [];

});
};