mongoose多个连接
目前,我的连接 具有以下代码:
var mongoose = require('mongoose');var uriUtil = require('mongodb-uri');
var mongodbUri = 'mongodb://localhost/db_name';
var mongooseUri = uriUtil.formatMongoose(mongodbUri);
mongoose.connect(mongooseUri);
module.exports = mongoose;
需要连接的文件是 :
var mongoose = require('../model/mongoose');var schema = mongoose.Schema({...});
如何更新mongoose.js以使用mongoose.createConnection(…)函数使用多个连接?
当我进行如下更改时,我仅从一个连接的更改开始:
var mongoose = require('mongoose');mongoose.createConnection('mongodb://localhost/db_name');
mongoose.open('localhost');
module.exports = mongoose;
我得到“未定义不是函数”。如果我使用此代码:
var mongoose = require('mongoose');db = mongoose.createConnection('mongodb://localhost/db_name');
db.open('localhost');
module.exports = mongoose;
我收到“错误:尝试打开未关闭的连接”
有什么建议吗?
回答:
mongoose通过 _连接池_处理连接http://mongoosejs.com/docs/connections.html
您可以使用server: {poolSize: 5}
选项增加/减少池(并行连接数)
如果您需要连接到不同的数据库,请在此处查看Mongoose和单个node.js项目中的多个数据库
多个连接的示例:
var mongoose = require('mongoose')var conn = mongoose.createConnection('mongodb://localhost/db1');
var conn2 = mongoose.createConnection('mongodb://localhost/db2');
var Schema = new mongoose.Schema({})
var model1 = conn.model('User', Schema);
var model2 = conn2.model('Item', Schema);
model1.find({}, function() {
console.log("this will print out last");
});
model2.find({}, function() {
console.log("this will print out first");
});
以上是 mongoose多个连接 的全部内容, 来源链接: utcz.com/qa/405689.html