gpt4 book ai didi

node.js - 来自两个连接的 Mongoose 模型

转载 作者:太空宇宙 更新时间:2023-11-04 00:18:55 25 4
gpt4 key购买 nike

我有以下设置:

/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');
...

我想从同一个 app.js 文件访问 secondDB 模型,我该怎么做?

<小时/>

更新:最终设置应如下所示:
(不知道如何将连接传递给模型)

/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');
...

最佳答案

试试这个,谢谢!

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

  • 注意 - 我没有测试过。

关于node.js - 来自两个连接的 Mongoose 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45327392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com