Answer the question
In order to leave comments, you need to log in
Multiple databases in one NodeJS project with Mongoose?
Is it possible to use multiple Mongo databases in one NodeJS application?
How to switch between them? How to organize the code?
Here is an example of what I have now:
mongoose.connect(process.env.DATABASE, { useMongoClient: true, autoReconnect: true, reconnectTries: 30, reconnectInterval: 1000 }, )
.then(() => {
console.log('Connected to Server successfully!')
})
.catch((err) => {
console.log(` → ${err.message}`)
return err
})
process.env.DATABASE
and a collection is already created there according to my model. Answer the question
In order to leave comments, you need to log in
I'm at a loss as to why the same model is in different databases (it's clear if there are different models in different databases), but you can
1. create multiple database connections using mongoosejs.com/docs/api.html#index_Mongoose-create ...
2. and then within this connection create a model mongoosejs.com/docs/api.html#connection_Connection...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question