Java Spring mongo 动态地向和操作符添加条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23214464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:22:52  来源:igfitidea点击:

Spring mongo adding criteria to and operator dynamicaly

javaspringmongodbspring-mongo

提问by Prashant Thorat

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?

我在哪里失踪?

采纳答案by jmen7070

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

要通过“$and”运算符从标准列表中合并所有标准:

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

here is docs

这是文档