我正在尝试使用Spring Data MongoDB创建一个聚合管道,该管道将新的数组字段投射到管道中。如何使用Spring Data实现这一目标?
我试图复制的Pipeline阶段如下:
{
$project: {
"aceId": 1,
"startActivityDateTime": 1,
"lastActivityDateTime": 1,
"eventInfo": [
"$applicationInfo",
"$riskAssessmentInfo",
"$policyInfo",
"$submissionInfo"
]
}
},
答案 0 :(得分:2)
我遇到了同样的问题并找到了解决方案。你必须创建一个CustomAggregationOperation类,如下所示: https://stackoverflow.com/a/29186539/5033846
public class CustomProjectAggregationOperation implements AggregationOperation {
private DBObject operation;
public CustomProjectAggregationOperation (DBObject operation) {
this.operation = operation;
}
@Override
public DBObject toDBObject(AggregationOperationContext context) {
return context.getMappedObject(operation);
}
}
然后您可以按如下方式实现投影阶段:
new CustomAggregationOperation(new BasicDBObject("$project",
new BasicDBObject("aceId", 1)
.appending("startActivityDateTime", 1)
.appending("lastActivityDateTime", 1)
.appending("eventInfo",
new Object[]{
"$applicationInfo",
"$riskAssessmentInfo",
"$policyInfo",
"$submissionInfo"}
)
));