Prior to Spring Data MongoDB 1.9.0-RELEASE, I was able to create a MongoTemplate object as follows:
new MongoTemplate(client, dbName, credentials)
. Upon upgrading, this constructor no longer works, giving an error to use MongoCredential
instead. However, there is no similar MongoTemplate
constructor that uses MongoCredential
. It appears that the only way to specify credentials now is when constructing the MongoClient
object.
However, since my app is multitenant on the database level, this doesn't work because it does not allow for additional credentials to be added after construction (meaning MongoTemplate
s cannot be created dynamically). It also is not ideal because if any of the credentials in the list are bad, none of the database connections work, as opposed to just the one with bad credentials.
I also do not want to create a new MongoClient
instance for each database. From what I understand, doing so would create a new connection for each database rather than letting MongoClient
manage a connection pool, which is ultimately not sustainable since Mongo only allows a finite number of connections.
Do I have any options here besides continuing to use the outdated library?
What I ended up doing is creating a single user in the admin
database that has access to all of the databases that I need (achieved via the roles
array). I create one MongoClient
, authorizing as that user against the admin
database. Then I am able to create MongoTemplate
objects dynamically without issue, because the user I'm authorized as hasreadWrite
permissions on those databases.