Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring mongo adding criteria to and operator dynamicaly

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?

like image 825
Prashant Thorat Avatar asked Apr 22 '14 08:04

Prashant Thorat


1 Answers

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

like image 60
jmen7070 Avatar answered Nov 16 '22 01:11

jmen7070