Node.js - Mongoose - connect 不能多個db連線問題

摘要:Node.js + Mongoose - connect 不能多個db連線問題

今天我想用Mongoose,連兩個database

出現Trying to open unclosed connection問題

 

網路查解後,發現用mongoose.connect,只能連一個,

若要連兩個的話,需要使用createConnection

 

用法如下

var mongoose = require('mongoose'),

       config = require('./config');

var opts = { server: { auto_reconnect: false,poolSize: 4}, user: config.user, pass:config.pass};

var conn = mongoose.createConnection(config.host,config.database,config.port,opts);

 

取得Model則用

var Model = conn.model(collection_name,ModelSchema);