簡體   English   中英

AggregationResults類型強制轉換MongoDB + Spring數據+ Aggregation

[英]AggregationResults Type Cast MongoDB + Spring data + Aggregation

我正在使用聚合從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);

大災變,您的回答並不完全正確,但給了我解決問題的鑰匙,這是我必須要做的

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

非常感謝,Kike。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM