How to connect secondary mongo DB to your node js app

//It will connect with your mongo db 

function makeNewConnection(uri) {
  const db = mongoose.createConnection(uri, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  });
  return db;
}


const newDbConnection = makeNewConnection("dblink");
//if your db is connection then we write the bottm logic

newDbConnection.on("connected", async () => {
  const metaData = [];
  const collections = await newDbConnection.db.listCollections().toArray();
  //collection return all the collection in your app

  for (const collection of collections) {
    const collectionName = collection.name;
    //if you want invidiudual item in your app
    const individualSchema =
      await newDbConnection.db.collection(collectionName).findOne();
    make(individualSchema, collectionName)
  }

  await createMetaDataModel(metaData)
  function make(individualSchema, collectionName) {
    const keyValue = {};
    for (const field in individualSchema) {
      if (!keyValue[field]) {
        keyValue[field] = typeof (individualSchema[field]);
      }
    }
    const dbColumn = {
      collectionName,
      fields: keyValue
    }
    metaData.push(dbColumn);
  }

The second approach looks like this

//it will conect to your mongo db url
function makeNewConnection(uri) {
  const db = mongoose.createConnection(uri, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  });
  return db;
}

const userConnection = makeNewConnection("mongodb://127.0.0.1:27017/users");
const todoConnection = makeNewConnection("mongodb://localhost/Aquaduct");

module.exports = {
  userConnection,
  todoConnection,
};



//in your app you can use like this
const mongoose = require('mongoose');
const {userConnection, todoConnection} = require('./connections');

const userSchema = new mongoose.Schema({
    name: String,
    isActive: Boolean,
}, {
    versionKey: false,
    timestamps: true,
});

const todoSchema = new mongoose.Schema({
    title: String,
    completed: Boolean,
}, {
    versionKey: false,
    timestamps: true,
});

const userModel = userConnection.model('User', userSchema);
const todoModel = todoConnection.model('Todo', todoSchema);

module.exports = {
    userModel,
    todoModel,
};