title: Connecting Multiple Mongo DBs in a Node.js Project
body: <p>I am trying to connect multiple MongoDB databases into a single Node.js project. Here is my current structure and issue at hand.</p>
  
  <p>Node Version: <strong>v6.12.1</strong></p>
  
  <p>Express.js Version: <strong>4.16.2</strong></p>
  
  <p>Mongoose Version: <strong>4.13.6</strong></p>
  
  <p>Current Structure:</p>
  
  <p><strong>primaryDB.js</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var mongoose = require('mongoose');
  var configDB = require('./database.js');
  
  
  //Connect to MongoDB via Mongoose
  mongoose.Promise = require('bluebird');
  
  //mongoose.Promise = global.Promise;
  mongoose.connect(configDB.url, { useMongoClient: true });
  
  
  //Check for successful DB connection
  var db = mongoose.connection;
  db.on('error', console.error.bind(console, 'connection error:'));
  db.once('open', function() {
    console.log("Primary DB Successfully Connected..");
  });
  
  module.exports = mongoose;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>secondaryDB.js</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var mongoose = require('mongoose');
  
  mongoose.connect('mongodb://mongodb_address_goes_here:27017/db_name', { useMongoClient: true });
  
  var db = mongoose.connection;
  db.on('error', console.error.bind(console, 'connection error:'));
  db.once('open', function() {
    console.log("Secondary DB Successfully Connected..");
  });
  
  
  module.exports = mongoose;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Then each DB connection gets imported respectively into their schema files, from which the schema files have module exports.</p>
  
  <p><strong>Issue at hand</strong></p>
  
  <p>When I run my application it starts fine and connects to both DB's successfully however I believe that mongoose is either getting overwritten or something because I might be able to do a <code>findOne()</code> command on primary but secondary fails or vice versa.</p>
  
  <p><strong>Example:</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var async = require('async');
  var primaryModel = require('../../../models/general/primary');
  var SecondaryModel = require('../../../models/general/secondary');
  
  
  function getInfo() {
  
    async.waterfall([
      getPrimaryName,
      getSecondaryName
    ], function (err, info) {
    });
  };
  
  function getPrimaryName(callback){
    Primary.findOne({}, function (err, primaryInfo){
      if (err) {
        console.log("Error" + err);
      }
        console.log('Primary info is : ' + primaryInfo);
       callback(null,primaryInfo);
    });
  }
  
  
  
  function getSecondaryName(primaryInfo, callback) {
    
    console.log(primaryInfo); //Make sure its being passed
  
    Secondary.findOne({}, function (err, secondaryInfo) {
      if (err) {
        console.log("Error" + err);
      }
      console.log('Secondary Info is  : ' + secondaryInfo);
      callback(null, secondaryInfo);
    });
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>The problem with above is I might get data back from the call to Primary but not Secondary. Which again I believe is from something being overridden .</p>
  
  <p>Any help appreciated. Sorry about the verbosity.</p>