我有以下设置:
/models/index.js
var glob = require('glob');
var mongoose = require('mongoose');
var Promise = require('bluebird');
mongoose.Promise = Promise;
mongoose.connect('mongodb://localhost/firstDB', {
useMongoClient: true
});
var models = glob.sync(__dirname + '/*.js', {ignore: '**/index.js'});
models.forEach(function (model) {
require(model);
});
/models/movie.js
var mongoose = require('mongoose');
var movieSchema = new mongoose.Schema({
title: String
}, {timestamps: true});
mongoose.model('movie', movieSchema);
/app.js
require('./models');
...
var Movie = mongoose.model('movie');
...
我想从同一个secondDB
文件访问app.js
个模型,我该怎么做?
更新:结束设置应如下所示:
(不确定如何将连接传递给模型)
/models/index.js
var glob = require('glob');
var mongoose = require('mongoose');
var Promise = require('bluebird');
mongoose.Promise = Promise;
var firstConnection = mongoose.connect('mongodb://localhost/firstDB', {
useMongoClient: true
});
var secondConnection = mongoose.connect('mongodb://localhost/secondDB', {
useMongoClient: true
});
var firstModels = glob.sync(__dirname + '/*.js', {ignore: '**/index.js'});
firstModels.forEach(function (model) {
require(model);
});
var secondModels = glob.sync(__dirname + '/second/*.js');
secondModels.forEach(function (model) {
require(model);
});
/models/movie.js
var mongoose = require('mongoose');
var movieSchema = new mongoose.Schema({
title: String
}, {timestamps: true});
mongoose.model('movie', movieSchema);
/models/second/car.js
var mongoose = require('mongoose');
var carSchema = new mongoose.Schema({
color: String
}, {timestamps: true});
mongoose.model('car', carSchema);
/app.js
require('./models');
...
var Movie = mongoose.model('movie');
var Car = mongoose.model('car');
...
答案 0 :(得分:1)
试试这个,谢谢!
var mongoose = require('mongoose')
var connection1 = mongoose.createConnection('connectionstring1');
var connection2 = mongoose.createConnection('connectionstring2');
检查此网址 - http://mongoosejs.com/docs/connections.html
并转到多重连接部分。
修改:
<强> connectionOne.js 强>
var mongoose = require('mongoose'),
mongoURI = 'mongodb://user:password@localhost:27017/dbOne';
module.exports = connectionOne = mongoose.createConnection(mongoURI);
connectionOne.on('connected', function() {
console.log('Mongoose connected to connectionOne');
});
require('./events')
<强> connectionTwo.js 强>
var mongoose = require('mongoose'),
mongoURI = 'mongodb://user:password@localhost:27017/dbTwo';
module.exports = connectionTwo = mongoose.createConnection(mongoURI);
connectionTwo.on('connected', function() {
console.log('Mongoose connected to connectionTwo');
});
require('./events')
<强> events.js 强>
var mongoose = require('mongoose'),
connectionOne = require('./connectionOne');
var Event = new mongoose.Schema({
name: { type: String, required: true },
date: { type: Date, required: false }
});
var connectionTwo = require('./connectionTwo');
var newEvent = new mongoose.Schema({
name: { type: String, required: true },
organizer: { type: String, required: true }
});
module.exports = {
connectionOne.model('Event1', Event);
connectionTwo.model('Event2', newEvent);
}
更多解释 - handling-multiple-databases-and-connections-with-mongoose