Last active
November 17, 2022 14:31
-
-
Save mdalishanali/cba3b36dab4ec143f4248c500a6cdc78 to your computer and use it in GitHub Desktop.
How to connect multiple mongo db to your node js app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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, | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment