MongoDB
 sql >> база данни >  >> NoSQL >> MongoDB

Как да създадете Aggregation от списък с AggregationOperation в Spring данни MongoDB?

Ето решението, което работи добре:

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


}



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Удостоверяване по време на свързване към сървър на MongoDB с помощта на Java

  2. MongoDB $acos

  3. MongoDB - Еквивалент на LEFT JOIN, където една колекция не съществува

  4. проблеми с използването на обещания с mongoose

  5. Как да създадете схема Mongoose с масив от идентификатори на обекти?