Spring mongo adds criteria and operator dynamics

I am trying to create a dynamic query using input and working with the user. My code I created a list of criteria, for example:

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

and added criteria to this list. And its addition is successful. Now I want to make an operator between each criterion.

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

It works fine But my input is not fixed, so I want it to add dynamically, I tried as

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

where am i out

+4
source share
1 answer

To combine all the criteria from the criteria list using the "$ and" operator:

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

here docs

+9
source

Source: https://habr.com/ru/post/1537475/


All Articles