I am using NuxtJS 3 and Vue 3 and I am connecting to a single mongoDb database without problem. I would like to connect at the same time to another database, where I will store and retrieve data.
My question is: How can I implement that and selecting each database in the server routes?
I am using mongoose!
2
Answers
.env file:
nuxt.config.ts:
server.index.ts:
But I do not know, how to define which db to use in mongoose models / in the server requests
Thanks for elaborating on your question, after making both connections to mongoose, you can use mongoose.connections to differentiate between your databases!
ex:
To simplify this further, I would create a variable that holds these values so that you can use them in your routes without repeating code!
Like this:
Here’s a link to the documentation !
https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.connections