【问题标题】:How to create an Aggregation from a list of AggregationOperation in Spring data MongoDB?如何从 Spring Data MongoDB 中的 AggregationOperation 列表创建聚合?
【发布时间】:2016-12-14 10:38:40
【问题描述】:

我想创建一个可以在 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);

但是这样做,我在最后一行得到一个编译时错误,如下所示:

构造函数 Aggregation(List) 不是 可见

原因是因为 Aggregation(List) 构造函数具有受保护的访问权限。 有没有我可以通过我的 AggregationOperation 列表来创建聚合? 有什么建议吗?

【问题讨论】:

  • AggregationResults 结果 = mongoOperation.aggregate(Aggregation.newAggregation(aggregationOperations), "gui_data", AggregateFactoryResult.class);

标签: java mongodb java-8 aggregation spring-data-mongodb


【解决方案1】:

以下是效果很好的解决方案:

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;
    }


}

【讨论】:

    猜你喜欢
    • 2015-12-17
    • 2020-04-14
    • 2014-01-30
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多