var mongodbHost = 'mongodb://localhost:9000/';
var connectionA = require('mongoose').createConnection(mongodbHost + 'A', function(err){
var schema = require(path.resolve(__dirname, 'migrations', 'v01_00_001', 'models', 'index')).schemas.Serial;
debugger;
connectionA.model('Test', schema);
console.log('First check:', schema.statics.___XXX === connectionA.models.Test.___XXX);
}.bind(this));
var connectionB = require('mongoose').createConnection(mongodbHost + 'B', function(err){
var schema = require(path.resolve(__dirname, 'models', 'index')).schemas.Serial;
debugger;
connectionB.model('Test', schema);
console.log('First check:', schema.statics.___XXX === connectionA.models.Test.___XXX);
console.log('Second check:', schema.statics.___XXX === connectionB.models.Test.___XXX);
}.bind(this));
// output is true, false, false
// While it should be: true, false, true
有人知道我会做什么吗?问题是,当我执行第一个 require('mongoose') 时,我得到了一个新的 mongoose 实例,因此其余的 requires 也将得到相同的实例。猫鼬在连接之间共享模式:s
任何帮助表示赞赏,谢谢