方法在类型ArrayList中添加(过滤器)<;过滤器>;是否不适用于使用java的mongodb中的参数(Bson)? ArrayList fieldList=新的ArrayList(); 添加(Filters.lte(“ID”,5)); fieldList.add(Filters.lt(“amount”,500)); Bson group=Aggregates.group(“$NAME”,id,NAME,amount); 列表结果=collection.aggregate(Arrays.asList(Aggregates.match((Bson)fieldList),Aggregates.group(group))).into(new ArrayList());//此处发生错误

方法在类型ArrayList中添加(过滤器)<;过滤器>;是否不适用于使用java的mongodb中的参数(Bson)? ArrayList fieldList=新的ArrayList(); 添加(Filters.lte(“ID”,5)); fieldList.add(Filters.lt(“amount”,500)); Bson group=Aggregates.group(“$NAME”,id,NAME,amount); 列表结果=collection.aggregate(Arrays.asList(Aggregates.match((Bson)fieldList),Aggregates.group(group))).into(new ArrayList());//此处发生错误,java,mongodb,Java,Mongodb,如何解决此问题?您可以使用和过滤器添加更多过滤器 ArrayList<Filters> fieldList = new ArrayList(); fieldList.add(Filters.lte("ID", 5)); fieldList.add(Filters.lt("amount", 500)); Bson group = Aggregates.group("$NAME",id,name,amount); List<Object> results = collect

如何解决此问题?

您可以使用
过滤器添加更多过滤器

ArrayList<Filters> fieldList = new ArrayList();
fieldList.add(Filters.lte("ID", 5));
fieldList.add(Filters.lt("amount", 500));
Bson group = Aggregates.group("$NAME",id,name,amount); 
List<Object> results = collection.aggregate(Arrays.asList( Aggregates.match((Bson) fieldList), Aggregates.group(group) )).into(new ArrayList<>());// error occurring here
Bson match=Aggregates.match(Filters.and(Filters.lte(“ID”,5),Filters.lt(“amount”,500));
Bson group=Aggregates.group(“$NAME”,id,NAME,amount);
List results=collection.aggregate(Arrays.asList(match,group),Document.class).into(new ArrayList());

您可以使用
过滤器添加更多过滤器

ArrayList<Filters> fieldList = new ArrayList();
fieldList.add(Filters.lte("ID", 5));
fieldList.add(Filters.lt("amount", 500));
Bson group = Aggregates.group("$NAME",id,name,amount); 
List<Object> results = collection.aggregate(Arrays.asList( Aggregates.match((Bson) fieldList), Aggregates.group(group) )).into(new ArrayList<>());// error occurring here
Bson match=Aggregates.match(Filters.and(Filters.lte(“ID”,5),Filters.lt(“amount”,500));
Bson group=Aggregates.group(“$NAME”,id,NAME,amount);
List results=collection.aggregate(Arrays.asList(match,group),Document.class).into(new ArrayList());

由@Veeram给出的解决方案

解决方案:

Bson match = Aggregates.match(Filters.and(Filters.lte("ID", 5), Filters.lt("amount", 500)));
Bson group = Aggregates.group("$NAME", id, name, amount);
List<Document> results = collection.aggregate(Arrays.asList(match, group), Document.class).into(new ArrayList<>());
List fieldList=new ArrayList();
添加(Filters.lte(“ID”,5));
fieldList.add(Filters.lt(“amount”,500));
Bson match=Aggregates.match(Filters.and(fieldList));

通过此操作,我可以将所有过滤条件添加到列表中。

由@Veeram给出的解决方案有效

解决方案:

Bson match = Aggregates.match(Filters.and(Filters.lte("ID", 5), Filters.lt("amount", 500)));
Bson group = Aggregates.group("$NAME", id, name, amount);
List<Document> results = collection.aggregate(Arrays.asList(match, group), Document.class).into(new ArrayList<>());
List fieldList=new ArrayList();
添加(Filters.lte(“ID”,5));
fieldList.add(Filters.lt(“amount”,500));
Bson match=Aggregates.match(Filters.and(fieldList));

通过此操作,我可以将所有筛选条件添加到列表中。

您向我们显示的错误发生在哪一行代码中?您需要Bson的arraylist。类似于
List fieldList=new ArrayList()
我已经尝试过了,得到了错误:java.util.ArrayList无法转换为org.bson.conversions.bson@Veeramin您向我们展示了哪行代码会发生错误?您需要bson的ArrayList。类似于
List fieldList=new ArrayList()
我已经尝试过了,得到了错误:java.util.ArrayList无法强制转换为org.bson.conversions.bson@Veeramcant我可以将所有筛选条件添加到列表中,因为我永远不知道有多少条件(它们来自json)@VeeramYou仍然一次向列表中添加一个。您的json看起来怎么样?您如何知道对哪个字段使用哪个运算符?我还没有考虑如何在json中传递参数,是否可以一次将所有筛选条件添加到列表中,这样我就可以在列表中循环并单独添加到Aggregates.match()。有什么建议吗@如果你喜欢的话,你可以。您的意思是这样的
List fieldList=new ArrayList();添加(Filters.lte(“ID”,5));fieldList.add(Filters.lt(“amount”,500));Bson match=Aggregates.match(Filters.and(fieldList))
?我不能将所有筛选条件添加到列表中,因为我永远不知道有多少条件(它们来自json)@veeram您仍在一次将一个条件添加到列表中。您的json看起来怎么样?您如何知道对哪个字段使用哪个运算符?我还没有考虑如何在json中传递参数,是否可以一次将所有筛选条件添加到列表中,这样我就可以在列表中循环并单独添加到Aggregates.match()。有什么建议吗@如果你喜欢的话,你可以。您的意思是这样的
List fieldList=new ArrayList();添加(Filters.lte(“ID”,5));fieldList.add(Filters.lt(“amount”,500));Bson match=Aggregates.match(Filters.and(fieldList))