I am facing an issue regarding MongoDB multi-tenancy. I have two different mongoDB databases (db1 and db2). These both have different credentials.
db1 credentials:
userName: admin
password: passwd
db2 credentials:
userName: admin1
password: passwd1
I need to switch from one database to other at run-time. I have autowired mongoTemplate with db1 credentials, but now I am unable to update the template with db2 credentials. Is this possible? If yes, how? If not, please tell me any other way to switch the databases at run-time with different credentials.
Note that, I am aware of "SimpleMongoDbFactory". One can extend "SimpleMongoDbFactory" and can override "getDb" method and pass the required dbName in super.getDb("dbName") for multitenancy. But, this does not work with two databases with different credentials.
What if you create a MongoCredential
for each DB and pass them to a MongoClient
that you pass to your SimpleMongoDbFactory
MongoCredential credential1 = MongoCredential.createCredential("admin", db1, "password");
MongoCredential credential2 = MongoCredential.createCredential("admin1", db2, "password1");
MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential1, credential2));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With