java - AggregationResults 类型转换 MongoDB + Spring 数据 + 聚合

标签 java mongodb spring-data spring-data-mongodb

我使用聚合从 Spring 数据应用程序中询问 mongodb,当我得到结果时,我需要将其转换(或转换)为我自己的对象或类型,但我不知道如何操作。

这是我的代码

AggregationResults aggregationResults = DBManager.getInstance().getMongoOperation().aggregate(aggregation, LevelEntity.class,Object.class);

我想要类似的东西

AggregationResults<LevelList> aggregationResults = DBManager.getInstance().getMongoOperation().aggregate(aggregation, LevelEntity.class,LevelList.class);

它给了我这些信息

{
        "_id" : {
                "date" : "24/03/2015"
        },
        "levels" : [
                {
                        "_id" : ObjectId("54f8627071fdac0ec132b4e5"),
                        "_class" : "org.xxxxxxxxxxx.persistence.model.impl.LevelEntity",
                        "user" : {
                                "_id" : ObjectId("54da19ce71fd56a173a3451a"),
                                ...
                                ...
                                ...
                        },
                        "level" : 4,
                        "date" : ISODate("2015-03-24T14:04:32.830Z"),
                        "dateFormatted" : "24/03/2015"
                },
                {
                        "_id" : ObjectId("54f8627071fdac0ec132b4f4"),
                        "_class" : "org.xxxxxxxxxxx.persistence.model.impl.LevelEntity",
                        "user" : {
                                "_id" : ObjectId("54e34bd671fde9071569650c"),
                                ...
                                ...
                                ...
                        },
                        "level" : 3,
                        "date" : ISODate("2015-03-24T14:04:32.866Z"),
                        "dateFormatted" : "24/03/2015"
                }
        ]
}

你能帮帮我吗????

非常感谢

最佳答案

使用方法如下

TypedAggregation<LevelList> aggregation=Aggregation.newAggregation(LevelList.class,
                match, sortOp, skipOp, limitOp);
AggregationResults<LevelList> data = mongoTemplate.aggregate(aggregation,
                COLLECTION_NAME, LevelList.class);

关于java - AggregationResults 类型转换 MongoDB + Spring 数据 + 聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28896878/

相关文章:

java - "No qualifying bean of type"用于 Spring Boot 中的 JPA 存储库

java - 使用 Joda DateTime 作为 Jersey 参数?

java - 空 arrayList 和空字符串抛出 StringIndexOutOfBounds 异常

mongodb - 在 MongoDB 中存储 GeoJSON 多边形

mongodb - 如何知道mongodb使用的是哪个存储引擎?

mongodb - Spring Data MongoDB 阻止保留某些字段

java - 面板重绘后 mouseListener 丢失

java.rmi.ServerException : RemoteException occurred in server thread (ClassNotFoundException)

angularjs - 如何在一个 app.get 请求中返回多个 Mongodb 集合?

java - Thymeleaf 没有看到来自 Controller 的引用