Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
How to connect to different database according to request in node js and mongoose

I am developing a SaaS application with multiple users. So I need to use appropriate database according to requesting user. Below is my basic database connection.

mongoose.connect(databaseLink, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
    useCreateIndex: true
})

We can use createConnection instead of connect to connect to multiple database. But for that I need to specify separate models. Here I need to share same model for each databases.

Edit

You can assume that required database name will be stored in req.dbName object. So when a user make a request from client1, req.dbName will equals to client1 and I need to use client1 database for that particular request.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can try useDb for switching between different database using the same connection pool

https://mongoosejs.com/docs/api/connection.html#connection_Connection-useDb

about 4 years ago · Juan Pablo Isaza Report

0

you can simply make an object and store all connection strings there and as per the need you can use the connection string

var connetion_string = { "user":"mongodb://localhost:27017/usercollection", "marks":"mongodb://localhost:27017/markscollection"

}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!