Multiple Mongoose Connections

I currently have this code for my mongoose.js connection:

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; 

A file that requires a connection, test.js :

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


How to update mongoose.js to use multiple connections with mongoose.createConnection (...) function?

I start with changes for only one connection when I make such changes:

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

I get "undefined is not a function". If I use this code:

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

I get "Error: attempt to open a closed connection"

Any tips?

+7
mongodb mongoose
source share
4 answers

Mongoose handles connections through the connection pool http://mongoosejs.com/docs/connections.html

You can use the server: {poolSize: 5} option to increase / decrease the pool (number of concurrent connections)

If you need connections to different databases, look here Mongoose and several databases in one node.js project

An example of several compounds:

 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"); }); 
+10
source share

OK In your example, I found a solution that fits my needs.

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); 

The only thing is whether it is normal to add connection objects to the mongoose object, or there may be a more elegant solution.

+4
source share

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 (custom class)

 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; 

user.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
source share

You can do it this way

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

Now you can add a model to each mongoose instance.

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

look at the document here

0
source share

All Articles