在平均申请中的租赁

我正在尝试在MEAN中创build一个多租户应用程序,根据某些variables的值,它应该select一个mongoDB连接forms的连接池,并对集合执行CURD操作。

var mongoose = require('mongoose'); var connectionPool = {}; connectionPool['dbName1'] = mongoose.createConnection('mongodb://localhost/database1'); connectionPool['dbName2'] = mongoose.createConnection('mongodb://localhost/database2'); connectionPool['dbName3'] = mongoose.createConnection('mongodb://localhost/database3'); 

连接对象在使用CURD操作时无法正常工作。

错误 – db.collection(…)。find(…).exe不是函数

谢谢。

您正在将mongodb API与mongoose混合使用。

由于您正在使用mongoose ,您可以创build附加到特定连接的模型:

https://stackoverflow.com/a/19475270/2013580

 var conn = mongoose.createConnection('mongodb://localhost/testA'); var conn2 = mongoose.createConnection('mongodb://localhost/testB'); // stored in 'testA' database var ModelA = conn.model('Model', new mongoose.Schema({ title : { type : String, default : 'model in testA database' } })); // stored in 'testB' database var ModelB = conn2.model('Model', new mongoose.Schema({ title : { type : String, default : 'model in testB database' } }));