我想创建一个可以在 MongoOperations 的aggregate() 函数中使用的聚合。 因此,为了创建聚合,我使用了 AggregationOperation 列表,如下所示:
ApplicationContext ctx = new AnnotationConfigApplicationContext(MongoConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
List<AggregationOperation> aggregationOperations = new ArrayList<AggregationOperation>();
aggregationOperations.add(new MatchOperation(Criteria.where("country").is("tigo")));
aggregationOperations.add(new UnwindOperation(Fields.field("myDetails")));
aggregationOperations.add(new MatchOperation(Criteria.where("myDetails.type").is("health")));
aggregationOperations.add(new SortOperation(new Sort(Sort.Direction.ASC, "myDetails.datetime")));
AggregationResults<AggregateFactoryResult> result = mongoOperation.aggregate(new Aggregation(aggregationOperations), "gui_data", AggregateFactoryResult.class);
但是这样做,我在最后一行收到编译时错误,如下所示:
The constructor Aggregation(List) is not visible
原因是因为 Aggregation(List) 构造函数具有 protected 访问权限。 我是否可以传递 AggregationOperation 列表来创建聚合? 有什么建议吗?
最佳答案
这是一个效果很好的解决方案:
public static void checkMongoOperations(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(MongoConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
AggregationOperation match = Aggregation.match(Criteria.where("country").is("tiro"));
AggregationOperation unwind = Aggregation.unwind("myDetails");
AggregationOperation match2 = Aggregation.match(Criteria.where("myDetails.type").is("health"));
AggregationOperation sort = Aggregation.sort(Sort.Direction.ASC, "myDetails.datetime");
AggregationOperation limit = Aggregation.limit(1);
Aggregation aggregation = Aggregation.newAggregation(match, unwind, match2, sort, limit);
System.out.println("Aggregation = "+aggregation);
AggregationResults<AggregateFactoryResult> output = mongoOperation.aggregate(aggregation, "gui_data", AggregateFactoryResult.class);
System.out.println("output = "+output.getMappedResults().get(0).getCountry());
}
并且需要实现结果情况,即 AggregationFactoryResult,如下所示:
package com.ex.mongo;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class AggregateFactoryResult {
String date;
String country;
String operator;
String server_ip;
ArrayList<Details> details;
public AggregateFactoryResult(String date, String country, String operator, String server_ip, ArrayList<Details> details){
super();
this.country = country;
this.operator = operator;
this.server_ip = server_ip;
this.details = details;
}
public AggregateFactoryResult(){
super();
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public String getServer_ip() {
return server_ip;
}
public void setServer_ip(String server_ip) {
this.server_ip = server_ip;
}
public ArrayList<Details> getDetails() {
return details;
}
public void setDetails(ArrayList<Details> details) {
this.details = details;
}
}
class Details{
String type;
String datetime;
ArrayList<Module> module;
public Details(String type, String datetime){
super();
this.type = type;
this.datetime = datetime;
this.module = new ArrayList<Module>();
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDatetime() {
return datetime;
}
public void setDatetime(String datetime) {
this.datetime = datetime;
}
public ArrayList<Module> getModule() {
return module;
}
public void setModule(ArrayList<Module> module) {
this.module = module;
}
}
class Module{
ArrayList<ModuleDetail> moduleDetail;
public Module(ArrayList<ModuleDetail> moduleDetail){
super();
this.moduleDetail = moduleDetail;
}
public ArrayList<ModuleDetail> getModuleDetail() {
return moduleDetail;
}
public void setModuleDetail(ArrayList<ModuleDetail> moduleDetail) {
this.moduleDetail = moduleDetail;
}
}
class ModuleDetail{
String module_name;
String live;
public ModuleDetail(String module_name, String live){
super();
this.module_name = module_name;
this.live = live;
}
public String getModule_name() {
return module_name;
}
public void setModule_name(String module_name) {
this.module_name = module_name;
}
public String getLive() {
return live;
}
public void setLive(String live) {
this.live = live;
}
}
关于java - 如何从 Spring data MongoDB 中的 AggregationOperation 列表创建聚合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41140491/