0

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.

RAHUL GUPTA
  • 49
  • 11
  • https://docs.spring.io/spring-data/data-document/docs/current/reference/html/#repositories.custom-implementations – Shiva Nov 23 '18 at 07:24
  • @secretsuperstar i want to genrate the query and want to pass. i don't think that link is helpfull . but thanks sir – RAHUL GUPTA Nov 23 '18 at 07:44

1 Answers1

1

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.

aurelius
  • 3,946
  • 7
  • 40
  • 73
  • can you tell me example of query criteria for this problem or code snippet with atleaset 2 key value pair ,Because i'm totally new in Mongo & query criteria – RAHUL GUPTA Nov 23 '18 at 10:01
  • And i thing JPA is only for Rdbms not for NoSql – RAHUL GUPTA Nov 23 '18 at 10:06
  • 1
    query criteria and mongo db https://www.baeldung.com/queries-in-spring-data-mongodb – aurelius Nov 23 '18 at 10:15
  • 1
    map as request param is here: https://stackoverflow.com/questions/47418489/spring-mvc-populate-requestparam-mapstring-string – aurelius Nov 23 '18 at 10:18
  • can anyone tell me logic as in code of this because i'm not able to get the solution of this problem – RAHUL GUPTA Nov 23 '18 at 12:59
  • dude, you have all the material in the comments. First you need to map the request params to a map. Then you will end up in the repository. You will iterate over the map entries. And based on the entries you will create the query criteria. That's all :). – aurelius Nov 23 '18 at 13:43