【问题标题】:AggregationResults Type Cast MongoDB + Spring data + AggregationAggregationResults 类型 Cast MongoDB + Spring 数据 + 聚合
【发布时间】:2015-03-06 10:29:45
【问题描述】:

我正在使用聚合从我的 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"
                }
        ]
}

你能帮帮我吗!!???

非常感谢

【问题讨论】:

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


    【解决方案1】:

    如下使用

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

    【讨论】:

    • 大灾变你可能不知道,但“我爱你”;)
    【解决方案2】:

    好吧,大灾变你的回答并不完全正确,但给了我解决问题的关键,这是我必须做的

    TypedAggregation<LevelEntity> aggregation = Aggregation.newAggregation(LevelEntity.class,
                    userMatchOperation, dateMatchOperation, group(LevelEntity.DATE_FORMATTED_FIELD).push(ROOT).as(LevelByDate.ENTITY_NAME));
            AggregationResults<LevelByDate> data = DBManager.getInstance().getMongoOperation().aggregate(aggregation, HappinessByDate.class);
    

    这是 LevelByDate 对象

    @Document
    public class LevelByDate {
    
        public static final String ENTITY_NAME = "levelEntity";
    
        @Id
        private String id;
        List<LevelEntity> levelEntity;
    
        public LevelByDate() {
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public List<LevelEntity> getLevelEntity() {
            return levelEntity;
        }
    
        public void setLevelEntity(List<LevelEntity> levelEntity) {
            this.levelEntity = levelEntity;
        }
    }
    

    非常感谢, 凯克。

    【讨论】:

      猜你喜欢
      • 2017-07-14
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      • 2021-04-03
      • 2017-02-18
      • 2017-04-23
      • 2018-10-28
      • 2021-09-03
      相关资源
      最近更新 更多