我如何在猫鼬中访问另一个数据库的 schema ?
例如:db1 has users list. db2 has another list. My application has all the models defined only for db1. but now i want to access the list of db2 in my application
。我该如何实现?请帮助我。
答案 0 :(得分:1)
您可以在Connection
类型上使用useDb函数。它返回另一个连接。如果您共享相同的架构,则您的代码可能如下所示:
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/test');
var schema = new Schema({
// ...
});
var db = mongoose.connection;
var Model1 = db.model('schema', schema, 'colName');
var db2 = db.useDb('test2');
var Model2 = db2.model('schema2', schema, 'colName');