Java – spring Mongo adds standard operator and dynamic

I'm trying to create a dynamic query using user input and actions

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

And added the standard of this list And successfully added Now I want to make and operate between each standard

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

It works well, but my input hasn't been fixed, so I hope it should be added dynamically. I've tried

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

Where I disappeared?

Solution

Unify all standards in the standard list through the "$and" operator:

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

This is docs

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>