简体   繁体   中英

AggregationResults Type Cast MongoDB + Spring data + Aggregation

I'm asking mongodb from my Spring data application using aggregation and when I get the result I need to cast it (or transform it) to my own object or type and I don't know how.

This is my code

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

I would like to have something like

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

and it gives me back this information

{
        "_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"
                }
        ]
}

Can you please help me!!???

Thanks a lot

Use as follow

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

well Cataclysm your response is not totally correct but gave me the key to solve my problem, this is what I had to do

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

And this is the LevelByDate object

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

Thanks a lot, Kike.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM