如何从Spring数据MongoDB中的AggregationOperation列表创建聚合? [英] How to create an Aggregation from a list of AggregationOperation in Spring data MongoDB?

查看:3712
本文介绍了如何从Spring数据MongoDB中的AggregationOperation列表创建聚合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可以在MongoOperations的aggregate()函数中使用的聚合。
因此,为了创建聚合,我使用了一个AggregationOperation列表,如下所示:

I want to create an Aggregation that can be used in MongoOperations's aggregate() function. So for creating the Aggregation, I used a list of AggregationOperation as follows:

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

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

But doing this, i am getting a compile time error at the last line as follows:


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

The constructor Aggregation(List) is not visible

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

The reason is that because the Aggregation(List) constructor is having protected access. Is there any i can pass my list of AggregationOperation to create an aggregation? Any suggestion?

推荐答案

这是一个运作良好的解决方案:

Here is the solution which worked well:

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,如下所示: / p>

And there is a need to implement the result case i.e. AggregationFactoryResult as follows:

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


}

这篇关于如何从Spring数据MongoDB中的AggregationOperation列表创建聚合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆