[英]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 following但
aspectTemplates.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
类。
@Document(collection = "COLLECTION_NAME")
public class AspectTemplate {
@Id
private ObjectId id;
private String title;
private List<String> options;
}
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.