如何在MogoOperations聚合函数中定义输出类型?

时间:2016-12-14 10:13:12

标签: java mongodb spring-data-mongodb

我正在使用MongoOperations的聚合函数来使用Spring Data进行某种中级查询。 如aggregate()函数的文档中所定义: http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoOperations.html#aggregate-org.springframework.data.mongodb.core.aggregation.Aggregation-java.lang.Class-java.lang.Class-,outputType应为

  

outputType - 返回列表的参数化类型,不能为null。

但我不确定,我应该如何定义该类/对象。 这是我的疑问:

    ApplicationContext ctx = new AnnotationConfigApplicationContext(MongoConfig.class);
    MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate"); 

    AggregationOperation match = Aggregation.match(Criteria.where("country").is("tigo"));
    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);
    AggregationOutput output = mongoOperation.aggregate(aggregation, "gui_data", outputType);

outputType是我需要定义的。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我尝试了dto类,因此将返回值映射到该dto类将很有用