RAHUL GUPTA
RAHUL GUPTA

Reputation: 49

creating a custom query in Springboot mongodb(using crudRepository)

I want to Create custom query which will depend on number of Fields will get from @RequestParam Like

public List<User> method(
  @RequestParam("key") String []key,
  @RequestParam("value") String [] value){}

Then I have to create a query in Service layer according to number of element I'll get in key and value

For example in first array(in key) element is "UserName" then in value array I'll get (value of username)

In these arrays, the number of fields will be dynamic. After that I have to pass that whole query String in Repository of Springboot & retrieve data from mongodb database.

Upvotes: 0

Views: 1462

Answers (1)

aurelius
aurelius

Reputation: 4076

Firstly, I would use a map instead of two arrays to map the request params.

For dynamic queries you have to use query criteria or HQL.

Then based on the map entries I would build the query criteria from JPA.

There is already a lot of material on how to build a criteria.

Upvotes: 1

Related Questions