简体   繁体   中英

spring-data-mongodb. How can i dynamically create a database in mongo using spring-data-mongodb library?

spring-data-mongodb. How can i dynamically create a database in mongo using spring-data-mongodb library?

I am trying to use Spring-Mongodb-Data module for CRUD operations against Mongo database and going through examples and articles my assumption is that databasename should be pre-defined in spring context xml when defining MongoTemplate bean.

In my case I have an multi-tenant application that will accept requests over http and my application should create the mongodatabase on-the-fly and use the name provided in the input http request to create the database and then load the data into collection in the newly created database.

I am trying to figure out if there is a way to dynamically populate the databasename in MongoTemplate or MongoRepository without having to provide it in spring context.xml?

Please help me.

Thanks -RK

Have you tried the following instead of going through the pre-defined spring context configuration.

MongoTemplate getMongoTemplate(Mongo mongo, String database) {
    return new MongoTemplate(mongo, database);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM