Kerrin631 Kerrin631 - 5 months ago 14
Node.js Question

Creating multiple Mongoose DBs in modularized node application

I'm creating a node application that makes use of two Databases.
I am trying to implement the mongoose.createConnection() method. I can see how it could apply in a project that is not strictly modularized but when modularized this method is not calling correctly.

For example the below code makes use of the createConnection method pretty well if I kept all the logic in one file:

var conn = mongoose.createConnection('mongodb://localhost/testA');
var conn2 = mongoose.createConnection('mongodb://localhost/testB');

// stored in 'testA' database
var ModelA = conn.model('Model', new mongoose.Schema({
title : { type : String, default : 'model in testA database' }

// stored in 'testB' database
var ModelB = conn2.model('Model', new mongoose.Schema({
title : { type : String, default : 'model in testB database' }

Due to strict modularization practices my files are set up like this:


var mongoose = require('mongoose');
var fs = require('fs');

// connect to the database
var conn = mongoose.createConnection('mongodb://localhost/testA');
var conn2 = mongoose.createConnection('mongodb://localhost/testB');

// traditional way of making a single Mongo connection
// mongoose.connect('mongodb://localhost/surgeon_solutions');

// specify the path to all of the models
var models_path = __dirname + '/../models'

// read all of the files in the models_path and for each one check if it is a javascript file before requiring it
fs.readdirSync(models_path).forEach(function(file) {
if(file.indexOf('.js') > 0) {
require(models_path + '/' + file);


I have tried replacing 'mongoose.model('User', UserSchema);' with 'conn.model('User', UserSchema);' but this still does not work.

var mongoose = require('mongoose');
var crypto = require('crypto');
var jwt = require('jsonwebtoken');
var secret = "secret"

var UserSchema = new mongoose.Schema({
username: {type: String, lowercase: true, unique: true},
email: {type: String, lowercase: true, unique: true},
hash: String,
salt: String

UserSchema.methods.setPassword = function(password){
this.salt = crypto.randomBytes(16).toString('hex');

this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');

UserSchema.methods.validPassword = function(password) {
var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');

return this.hash === hash;

UserSchema.methods.generateJWT = function() {
var today = new Date();
var exp = new Date(today);
exp.setDate(today.getDate() + 1);

return jwt.sign({
_id: this._id,
username: this.username,
exp: parseInt(exp.getTime() / 1000),
}, secret);

mongoose.model('User', UserSchema);


var crypto = require('crypto');
var mongoose = require('mongoose'),
User = mongoose.model('User');
var passport = require('passport');
var jwt = require('express-jwt');
var secret = 'secret';
var auth = jwt({secret: secret, userProperty: 'payload'});

module.exports = {
Register: function(req, res, next){
if(!req.body.username || !req.body.password){
return res.status(400).json({message: 'Please fill out all fields'});
User.findOne({username: req.body.username}, function(err, user){
// console.log(err)
return res.json({error: 'This username already exists.'})
} else {
var user = new User();
user.username = req.body.username;
user.setPassword(req.body.password); = (; (err){
return next(err);
return res.json({token: user.generateJWT()})

application structure

-Root Folder/


I think you have forgot to export your User model.

Replace :

mongoose.model('User', UserSchema);


var User = mongoose.model('User', UserSchema);
module.exports = User;

Using multiple connection:

Instead of mongoose.model, you need to use conn.model

replace :

User = mongoose.model('User');


User = conn.model('User');

and dont forget to define the conn in the same file.

var conn      = mongoose.createConnection('mongodb://localhost/testA');