Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hackinf0/4f97a6a7e820cb612e0ff5c39139d08f to your computer and use it in GitHub Desktop.
Save hackinf0/4f97a6a7e820cb612e0ff5c39139d08f to your computer and use it in GitHub Desktop.
Connecting Multiple Database in NodeJs with MongoDB and Mongoose
Connecting Multiple Database in NodeJs with MongoDB and Mongoose.
Mongoose doesn't allow to use multiple databases in single mongoose instance as the models are build on one connection.
While using multiple mongoose instances, Node.js will not allow multiple module instances as it has caching system in require().
we've tried to use createConnection() and openSet() in mongoose, but it was not the solution. So we create all from start to finish.
Here you can get the solution
Explanation file: model.js
The following code defines the data models to be used with each of the databases by associating them with a
previously defined schema (userSchema and qrSchema).
Data models are exported for use elsewhere in the code.
Explanation file: db.js
This code imports the mongoose library which allows to connect to a MongoDB database.
There are two URIs defined to connect to two different databases: MONGO_URI and MOBILE_URI.
Each URI contains connection information to a remote MongoDB database, including user credentials.
The connectDBs function uses the mongoose.createConnection method to create connections to these two
databases using the respective URIs. The useUnifiedTopology and useNewUrlParser options are set to enable the new
unified topology policy and use MongoDB's new URL parser, respectively.
The function returns an object that contains the connections to these two databases as qrCodeDb and userDB properties.
const mongoose = require('mongoose')
const MONGO_URI =
'mongodb+srv://owlhacking:[email protected]/qrCodeData?retryWrites=true&w=majority'
const MOBILE_URI =
'mongodb+srv://owlhacking:[email protected]/Cluster0?retryWrites=true&w=majority'
const connectDBs = () => {
try {
const qrCodeDb = mongoose.createConnection(MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true
})
const userDB = mongoose.createConnection(MOBILE_URI, {
useUnifiedTopology: true,
useNewUrlParser: true
})
return { qrCodeDb, userDB }
} catch (error) {
console.error(`Error:${error.message}`)
process.exit(1)
}
}
module.exports = { connectDBs }
const { default: mongoose } = require('mongoose')
const { connectDBs } = require('../config/db')
const qrSchema = mongoose.Schema({
qrInformation: {
type: String,
required: true
},
qrImage: {
type: String,
required: true
},
timeStamp: {
type: String,
default: Date()
}
})
const userSchema = new mongoose.Schema({
fullname: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
}
})
const { qrCodeDb, userDB } = connectDBs()
module.exports = {
userSchema: userDB.model('user', userSchema),
Qrcode: qrCodeDb.model('Qrcode', qrSchema)
}
@soleroks
Copy link

Useful, thanks!

@AbhishekNayak007
Copy link

The above trick really work
Thanks :)

@hackinf0
Copy link
Author

Useful, thanks!

You’re welcome😇

@hackinf0
Copy link
Author

The above trick really work Thanks :)

Glad that it has helped you😊

@syedamanat
Copy link

Useful; works, danke.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment