简体   繁体   English

将 Spring MongoDB AggregationResults ArrayList 映射到列表<POJO>

[英]Mapping Spring MongoDB AggregationResults ArrayList to List<POJO>

I need to return a List<AspectTemplate> from MongoDB aggregation operation.我需要从 MongoDB 聚合操作返回一个List<AspectTemplate>

public class AspectTemplate {
  private ObjectId id;
  private String title;
  private List<String> options;
}

In Spring MongoDB repository I am mapping AggregationResults like this在 Spring MongoDB 存储库中,我像这样映射AggregationResults

ProjectionOperation projectOperation = Aggregation.project()
            .and("easpects").concatArrays("iaspects").as("allaspects").andExclude("_id");
AggregationResults<AspectTemplate> aspectTemplates = this.mongoOperations.aggregate(Aggregation.newAggregation(
            matchOperation,
            lookupOperation, projectOperation
    ), COLLECTION_NAME, AspectTemplate.class);

return aspectTemplates.getMappedResults();

The raw results are原始结果是

在此处输入图片说明

But the aspectTemplates.getMappedResults() returns the followingaspectTemplates.getMappedResults()返回以下内容在此处输入图片说明

在此处输入图片说明

How can I return allaspects ArrayList seen in raw results as List<AspectTemplate ?如何将原始结果中看到的allaspects ArrayList 返回为List<AspectTemplate

You need to add 2 extra operators into your pipeline and suggestion to modify your Entity class.您需要在管道中添加 2 个额外的运算符并建议修改您的Entity类。

Entity实体

@Document(collection = "COLLECTION_NAME")
public class AspectTemplate {
  @Id
  private ObjectId id;
  private String title;
  private List<String> options;
}

Aggregation聚合

ProjectionOperation projectOperation = Aggregation.project()
    .and("easpects").concatArrays("iaspects").as("allaspects")
    .andExclude("_id");
UnwindOperation unwind = Aggregation.unwind("allaspects");
ReplaceRootOperation replaceRoot = Aggregation.replaceRoot("allaspects");
AggregationResults<AspectTemplate> aspectTemplates = mongoOperations.aggregate(Aggregation.newAggregation(
            matchOperation,
            lookupOperation, projectOperation,
            unwind, replaceRoot
    ), mongoOperations.getCollectionName(AspectTemplate.class), AspectTemplate.class);

return aspectTemplates.getMappedResults();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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