0

How to handle spring data mongo db dynamic query creation. I have a list that I need to filter by various filtering conditions (ie, sector wise , category wise , status wise , or sector & status combination list ).That is the user is having all kind of flexibility for filtering the list as his wish. So In spring data mongo db how to handle this kind of usecases. It is not possible to give all kind of combination using if .. else and execute different queries.

My mongodb version : v3.4.4

Model

@Document public class Investment extends AbstractMongoDocument {

private String legalInvestmentName;
private InvestmentCategory category;    => ENUM type
private InvestmentSector sector;
private InvestmentStatus status;
private InvestmentType type;
.................................

} From UI calling a method while clicking on Search Button

filterList(sector, category, type, status): Observable<any[]> {
    var params = new URLSearchParams();
    params.set("sector", sector);
    params.set("category", category);
    params.set("type", type);
    params.set("status", status);

    return this.http.get('investments/filterBy', { search: params })
      .map(this.extractData)
      .catch(this.handleError);

  }

Controller

@GetMapping(value = "/filterBy")
    @CrossOrigin(value = "*")
    public List<Investment> getInvestmentListFilterBy(@RequestParam(value = "sector", required = false) String sector, @RequestParam(value = "category", required = false) String category, @RequestParam(value = "type", required = false) String type, @RequestParam(value = "status", required = false) String status) throws InvestmentNotFoundException {
        String investorSector = "";
        String investmentCategory = "";
        String investmentType = "";
        String investmentStatus = "";
        if (sector != null && !sector.isEmpty()) {
            investorSector = InvestmentSector.convert(sector).name();
        } else if (category != null && !category.isEmpty()) {
            investmentCategory = InvestmentCategory.convert(category).name();
        } else if (type != null && !type.isEmpty()) {
            investmentType = InvestmentType.convert(type).name();
        } else if (status != null && !status.isEmpty()) {
            investmentStatus = InvestmentStatus.convert(status).name();

        }
        List<Investment> investmentList = null;
        investmentList = investmentService.findAllInvestmentFilterBy(investorSector, investmentCategory, investmentType, investmentStatus);
        return investmentList;
    }

Service

 @Override
    public List<Investment> findAllInvestmentFilterBy(String sector, String investmentategory, String type, String status) {
        List investmentList = null;
        if ((sector != null && !sector.isEmpty()) && (investmentategory != null && !investmentategory.isEmpty()) && (type != null && !type.isEmpty()) && (status != null && !status.isEmpty())) {

            investmentList = investmentRepository.filterBySectorCategoryTypeStatusAndCurrentStatus(sector, investmentategory, type, status, CurrentStatus.ACTIVE.name());

        } else if ((sector != null && !sector.isEmpty()) && (investmentategory != null && !investmentategory.isEmpty()) && (type != null && !type.isEmpty())) {
            investmentList = investmentRepository.filterBySectorCategoryTypeAndCurrentStatus(sector, investmentategory, type, CurrentStatus.ACTIVE.name());
        } else if ((sector != null && !sector.isEmpty()) && (investmentategory != null && !investmentategory.isEmpty())) {

            investmentList = investmentRepository.filterBySectorAndCategoryAndCurrentStatus(sector, investmentategory, CurrentStatus.ACTIVE.name());
        } else if ((sector != null && !sector.isEmpty())) {

            investmentList = investmentRepository.filterBySectorAndCurrentStatus(sector, CurrentStatus.ACTIVE.name());
        } else if ((investmentategory != null && !investmentategory.isEmpty())) {

            investmentList = investmentRepository.filterByCategory(investmentategory, CurrentStatus.ACTIVE.name());

        } else if ((type != null && !type.isEmpty())) {

            investmentList = investmentRepository.filterByTypeAndCurrentStatus(type, CurrentStatus.ACTIVE.name());
        } else if ((status != null && !status.isEmpty())) {

            investmentList = investmentRepository.filterByStatusAndCurrentStatus(status, CurrentStatus.ACTIVE.name());
        } else {
            investmentList = investmentRepository.getAllInvestmentsByCurrentStatus(CurrentStatus.ACTIVE.name());

        }

        return investmentList;

    }
Jim
  • 31
  • 1
  • 6
  • Please, post your code, the output you get and describe the desired output. This question, as it is, seems a bit too generic – Evil Toad Sep 19 '17 at 07:46
  • Possible duplicate of [Spring Data MongoDB Repository - JPA Specifications like](https://stackoverflow.com/questions/33735797/spring-data-mongodb-repository-jpa-specifications-like) – g00glen00b Sep 20 '17 at 07:02
  • Currently, I am executing different queries by checking null values. But this gives me the result for some combination of filter conditions. – Jim Sep 20 '17 at 11:20

0 Answers0