Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

How to do pipelining db connection

Hi team, got a question for you.

Here is the scenario… there will be standard (one mandatory) connection to one db, in this db, there is a collection which has customer db details(username,pwd). On each API call, there’ll be orgId that is passed as request body. Based on the orgId input, we should be fetching the relevant customer db details from the initial db. Based on the db details acquired from the DB call, we should connect to that particular DB and perform CRUD operation on the particular collection

Can we achieve this using mongorepository and spring boot?

Hello @shivananda_swamy,

you can configure to connect to a particular database (and there are many ways to configure). Repository calls can query a collection based upon an input (supplied) parameter value.The query results can be used to perform further actions.

For example, you can configure connection to a particular database using AbstractMongoClientConfiguration class and use with MongoRepository.

More details at: Spting Data MongoDB Reference.

Thanks for writing back Prasad.

What I am looking for right now is, not to go with mongoTemplate, and just using mongoRepository alone, just as we do with normal single connection mongodb where we have repository interface for one document.

To explain my problem more clearly, I obtain the connection to the 1stDB, from this DB I query for the particular collection to fetch customer db credentials and now using this details(mongodb credentials) I want to connect to that particular DB and then perform CRUD operation on the collections in that particular DB.

All this I want to achieve using mongoRepository way of spring boot application

Hope this explanation is helpful

There are two databases, and you are performing CRUD on the second database collections?

Yes Prasad
First database is used only for fetching second database credentials and nothing else

You will be able to do that. Using the latest of the Spring Data MongoDB software as well as the MongoDB server, facilitates latest features as well as developer APIs.

1 Like

Thanks for writing Prasad.

Could you help me out with documentation on this which could be helpful in tackling this challenge

In addition to the Spring Data MongoDB reference documentation I had already linked in my earlier post, these are useful documentation:

The Spring Data MongoDB reference documentation has code samples for most of the usage scenarios - as in your case using the MongoRepository API for configuration and operations on the MongoDB server.

You can always lookup online for specific articles, post queries, etc., at your favorite websites.