Search mongodb springdata
<p>suppose I want to search by multiple fields in spring-data-mongodb, how to make the query dynamic.
What I need to do is first check if the field value is not empty then use it in where clause.</p>

<p>Example,</p>

<pre><code>Query query=new Query();
if(isNotEmpty(searchDto.getA())){
    query.addCriteria(new Criteria().and("a").is(searchDto.getA()));
}
if(isNotEmpty(searchDto.getB())){
    query.addCriteria(new Criteria().and("b").is(searchDto.getB()));
}
if(isNotEmpty(searchDto.getC())){
    query.addCriteria(new Criteria().and("c").is(ssearchDto.getC()));
}
if(isNotEmpty(searchDto.getD())){
    query.addCriteria(new Criteria().and("d").is(searchDto.getD()));
}
query.with(new Sort(Sort.Direction.ASC, "a"));
return mongoTemplate.find(query, ResultDto.class, "MY_COLLECTION");
</code></pre>

<p>These is not fetching the data the size of the resultant list is always 0.</p>

<p>Is there any alternate way to do this.</p>