Database Selection At Runtime
akhtar-husain opened this issue · comments
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the issue has not already been raised
Issue
I have a mongodb cluster that has about 40 database that I need to selected based on some criteria.
Is there a way that I can use to select the database at runtime instead of passing in URI string ?
I have searched over internet and also in the issues & discord but couldn't find any answer.
Any help would be appreciated.
Could you use fastify.mongo.db(chooseDb(request))
?
https://mongodb.github.io/node-mongodb-native/4.1/classes/Db.html
Or do you have 40 different connections?
Thatnks @Eomm for the quick reply.
I have one connection with 40 different databases.
I can't call fastify.mongo.db("dbname")
, it shows error as Type 'Db' has no call signatures
@akhtar-husain >The fastify-mongodb
client
decorator should allow you to achieve what you want.
Can you try with something like this ?
const myMongoDatabase = fastify.mongo.client.db('dbname');
// your code here ...
ref.: Mongo documentation
Thank you @darkgl0w, it worked.