1

I want using @Query in Spring Data Mongodb

What is an equal for Spring Data MongoDb Query to this method?

I want to make a method to search based on the field and value that send by the someone when invoked method.

public List < Party > retrivePartyByQuery(Map<String, String> payload) {  
     Query query = new Query();         
     Iterator<Map.Entry<String, String>> iterator = payload.entrySet().iterator();
     while (iterator.hasNext()) {             
         Map.Entry<String, String> entry = iterator.next(); 

         List < Party > productDb = mongoTemplate.find(query,Party.class);

         return productDb;
    }
}
@Query(value = "{ 'field': 'value'}")
List<Admission> findByFieldAdmission(@Param("field")String field, @Param("value") String value);```
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • There is no Spring Boot JPA Mongodb. There is only Spring Data JPA and Spring Data MongoDb. The method you posted doesn't make any sense, which becomes obvious, once one formats it properly, which I did. I also added closing braces. – Jens Schauder Feb 06 '23 at 09:59
  • The `@Query` annotation of the various Spring Data modules essentially take a query String which has to be constant and be defined at compile time due to the constraints of annotations. For dynamic queries there are other mechanics, like `Criteria`, see https://stackoverflow.com/questions/24562481/build-dynamic-queries-with-spring-data-mongodb-criteria – Jens Schauder Feb 06 '23 at 10:06
  • Thank you Jens Schauder. I think i will use the mongo template to query based on criteria – Ahmad Irfaan Hibatullah Feb 13 '23 at 03:06

0 Answers0