2015-10-02 139 views
7

目前我有此代碼爲我的連接mongoose.js貓鼬多個連接

var mongoose = require('../model/mongoose'); 
var schema = mongoose.Schema({...}); 


:需要的連接 test.js

var mongoose = require('mongoose'); 
var uriUtil = require('mongodb-uri'); 
var mongodbUri = 'mongodb://localhost/db_name'; 
var mongooseUri = uriUtil.formatMongoose(mongodbUri); 
mongoose.connect(mongooseUri); 
module.exports = mongoose; 

文件

如何更新mongoose.js以使用多個co與mongoose.createConnection(...)函數的連接?

我開始只有一個連接的變化,當我做改變這樣的:

var mongoose = require('mongoose'); 
mongoose.createConnection('mongodb://localhost/db_name'); 
mongoose.open('localhost'); 
module.exports = mongoose; 

我得到「undefined是不是一個函數」。 如果我用這個代碼:

var mongoose = require('mongoose'); 
db = mongoose.createConnection('mongodb://localhost/db_name'); 
db.open('localhost'); 
module.exports = mongoose; 

我得到「錯誤:試圖打開未關閉連接」

有什麼建議?

回答

10

通過連接池貓鼬處理連接 http://mongoosejs.com/docs/connections.html

您可以使用server: {poolSize: 5}選項增加/減少池(多個並聯連接的)

如果您需要在不同的數據庫連接,看看這裏 Mongoose and multiple database in single node.js project

多連接示例:

var mongoose = require('mongoose') 
var conn = mongoose.createConnection('mongodb://localhost/db1'); 
var conn2 = mongoose.createConnection('mongodb://localhost/db2'); 
var Schema = new mongoose.Schema({}) 
var model1 = conn.model('User', Schema); 
var model2 = conn2.model('Item', Schema); 
model1.find({}, function() { 
    console.log("this will print out last"); 
}); 
model2.find({}, function() { 
    console.log("this will print out first"); 
}); 
+0

是的,我需要多個數據庫連接。我看到了這個答案,但在我的情況下仍然不起作用。 conn和conn2的例子看起來是最好的,但我不能將它分離爲兩個文件。你可以試一下嗎? – Pumych

+0

爲您增加了工作示例。這是你需要的嗎? – vmkcom

4

好的。以您的示例爲例,我找到了適合我需求的解決方案。

mongoose.js

var mongoose = require('mongoose'); 
mongoose.main_conn = mongoose.createConnection('mongodb://localhost/main'); 
mongoose.admin_conn = mongoose.createConnection('mongodb://localhost/admin'); 
module.exports = mongoose; 

content.js

var mongoose = require('../model/mongoose'); 
var schema = mongoose.Schema({...}); 

/// functions here 
schema.statics.func_a(){...}; 
schema.statics.func_b(){...}; 

// And finaly updated only one line 
//exports.Content = mongoose.model('Content', schema); 
exports.Content = mongoose.main_conn.model('Content', schema); 

的唯一的事情,是不是OK添加連接對象貓鼬對象,也可以有更多的優雅的解決方案。

0

config.js

module.exports = { 
    default: 'main', 
    main: 'mongodb://localhost/main', 
    admin: 'mongodb://localhost/admin', 
}; 

connection.js

const mongoose = require('mongoose'); 
const config = require('./config'); 

mongoose.Promise = global.Promise; 

function createConnection(name) { 
    return mongoose.createConnection(config[name]); 
} 

module.exports = createConnection(config[config.default]); 

module.exports.on = createConnection; 

model.js(自定義類)

const connection = require('./connection'); 

class Model { 
    constructor(name, data) { 
     this.data = data; 
     return this.connection().model(name, data.schema); 
    } 

    connection() { 
     if (this.data.connection) { 
      return connection.on(this.data.connection); 
     } 

     return connection; 
    } 
} 

module.exports = Model; 

用戶。JS

const Schema = require('mongoose').Schema; 
const conn = require('./connection'); 
const Model = require('./model'); 

const userSchema = new Schema({ 
    name: String, 
    email: String, 
    password: String 
}); 

// USING MONGOOSE MODEL 
// default connection 
const UserM1 = conn.model('User', userSchema); 

// admin connection 
const UserM2 = conn.on('admin').model('User', userSchema); 

// USING CUSTOM MODEL 
// default connection 
const UserC1 = new Model('User', { 
    schema: userSchema 
}); 

// admin connection 
const UserC2 = new Model('User', { 
    schema: userSchema, 
    connection: 'admin' 
}); 
0

你能做到這樣

var mongoose = require('mongoose'); 
var mongoose2 = new mongoose.Mongoose(); 

您現在可以添加模型到每個貓鼬實例。

mongoose.model('some', mongooseSchema1); 
mongoose.model('some2', mongooseSchema2); 

看看文檔here