0
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

任何帮助表示赞赏,谢谢

4

2 回答 2

0

https://github.com/LearnBoost/mongoose/issues/1211

var mongooseA = new (require('mongoose').Mongoose)();
var mongooseB = new (require('mongoose').Mongoose)();

然后创建连接2连接...

于 2012-11-15T09:12:53.633 回答
0

为模型指定不同的名称,但仍为两者使用相同的集合名称:

connectionA.model('TestA', schema, 'Tests');
...
connectionB.model('TestB', schema, 'Tests');
于 2012-11-15T03:38:35.413 回答