Question

I am trying to create dynamic query with user input with and operation My code is I created List of criteria like:

List<Criteria> criterias = new ArrayList<Criteria>();

and added criteria to this list.And its getting added successfully. now I want to make and operator between each criteria.

 Criteria criteria = new Criteria().andOperator(criterias.get(0), criterias.get(1));

It works fine But my input is not fixed so I want it should added dynamically, I tried like

for(int i=0;i<criterias.size();i++)
  Criteria criteria = new Criteria().andOperator(criterias.get(i));

where I am missing?

Was it helpful?

Solution

To unite all criterias from a list of criterias by "$and" operator :

Criteria criteria = new Criteria().andOperator(criterias.toArray(new Criteria[criterias.size()]));

here is docs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top