有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在研究MongoOperations的聚合函数,以使用Spring数据进行某种中级查询。 如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 - The parameterized type of the returned list, must not be 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);

输出类型是我需要定义的。有人能帮我吗


共 (1) 个答案

  1. # 1 楼答案

    我尝试了dto类,所以将返回值映射到该dto类将非常有用