0

I have a NodeJS app that currently utilizes a single mongo database, but I find myself in need of grabbing the data that is on a secondary database. Both databases are on the same cluster. The project structure is as follow:

    app.js
    models
    --Book.js
    --School.js
    --Class.js
    --User.js
    routes
    --adoptions.js
    views
    --handlebars files

In the app.js I've always used mongoose.connect() but I understood that this basically "maps" that mongoose connection with the global connection, so what I'm trying to use is mongoose.createConnection(). All the questions and answers I've found have the models and the connection objects in the same file, but in my project they are in different files. An other problem is that the models are not the same on the different databases: Database1 has only one collection of users, Database2 has Book, School and Class.

I'm struggling to retrieve data from the second database, even if I define a connection object in the routes file. I've tried to put:

var conn = mongoose.createConnection('connectionStringToDatabase2', {useNewUrlParser: true});
const Class = conn.model('class');
const Book = conn.model('book');
const School = conn.model('school');

inside my adoptions.js file but when I execute a find() query on any of them I get an empty array even though I know they contain data.

My model files look like this:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

//Create Schema
const BookSchema = new Schema({
    //Various schema stuff
}, { _id: false });

mongoose.model('book', BookSchema, 'book');

and the rest of the schemas follow the same structure.

How can I use the secondary connection, while still retaining my project structure? I don't really want to have one huge file with everything in it, and I know I'm missing something here

3
  • I would try const mongoose1 = require('mongoose'); and const mongoose2 = require('mongoose'); and see if you could have multiple modules loaded at the same time Commented Jul 12, 2019 at 1:33
  • @Ben But where would you put that? Commented Jul 12, 2019 at 7:55
  • You would put it where you initialize the mongoose instance Commented Jul 12, 2019 at 14:04

1 Answer 1

4

So I've managed to answer my own question after a lot of experiments:

I create a new database.js file in a config folder where I defined the secondary connection like so:

database.js

const mongoose = require('mongoose');

let conn = mongoose.createConnection('connectionStringToSecondaryDb', {useNewUrlParser: true});

module.exports = conn;

then I required this file in every model that was only defined in the second database like the documentation said, so:

Book.js

const mongoose = require('mongoose')
const conn = require('../config/database');
const Schema = mongoose.Schema;

//Create Schema
const BookSchema = new Schema({
    //Various schema stuff
}, { _id: false });

//Note here we are using the secondary conn object to model this schema
conn.model('book', BookSchema, 'book');

Having done this you can now just require the secondary connection in your routes file and use the models like normal:

SectionRoute.js

const express = require('express');
const router = express.Router();
const mongoose = require('../config/database');
require('../models/Book');

const Book = mongoose.model('book');

//Do normal mongoose use in your route file (Book.find(), Book.save, ecc ecc)

NOTE: in the sectionRoute I'm still using mongoose.model because I've set the mongoose const of that file as the connection passed by database.js, so it is not using the global mongoose.connection object!

Hope this helps out someone else as well!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.