Java 在两个级别上对操作进行分组,每个级别都有一个元素-数组关联
我正在使用AngularJS前端和java后端处理JHipster项目。我正在MongoDB数据库中使用Spring数据 我对名为Java 在两个级别上对操作进行分组,每个级别都有一个元素-数组关联,java,spring,mongodb,aggregation-framework,spring-mongodb,Java,Spring,Mongodb,Aggregation Framework,Spring Mongodb,我正在使用AngularJS前端和java后端处理JHipster项目。我正在MongoDB数据库中使用Spring数据 我对名为budgetCode的字符串字段执行了分组操作。因此,对于每个budgetCode,都有一个链接的taskCodes列表,这是另一个字符串字段。我通过另一篇Stackoverflow帖子成功地做到了这一点:“对字段进行分组操作,并将喜欢的字段列表放入数组” 此处,执行分组操作的aggregateAllTaskCode方法: @Override public List&
budgetCode
的字符串字段执行了分组操作。因此,对于每个budgetCode
,都有一个链接的taskCodes
列表,这是另一个字符串字段。我通过另一篇Stackoverflow帖子成功地做到了这一点:“对字段进行分组操作,并将喜欢的字段列表放入数组”
此处,执行分组操作的aggregateAllTaskCode方法:
@Override
public List<ClarityResourceAffectationReport> aggregateAllTaskCode() {
AggregationOperation project = new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
return new BasicDBObject("$project", new BasicDBObject("task_code", "$task_code").append("action_code", Arrays.asList("$action_code")));
}
};
Aggregation aggregation = newAggregation(project,
group("budgetCode", "taskCode").addToSet("actionCode").as("actionCodes"),
group("budgetCode").first("taskCode").as("taskCode").addToSet(new BasicDBObject("taskCode","$_id.taskCode").append("actionCodes", "$actionCodes")).as("taskCodes"),
sort(Sort.Direction.ASC, previousOperation(),"taskCodes"));
AggregationResults groupResults = mongoTemplate.aggregate(aggregation, ClarityResourceAffectation.class,
ClarityResourceAffectationReport.class);
List<ClarityResourceAffectationReport> clarityResourceAffectationReports = groupResults.getMappedResults();
log.debug("clarityResourceAffectationReports.size()" + clarityResourceAffectationReports.size());
log.debug("aggregation.toString()" + aggregation.toString());
return clarityResourceAffectationReports;
}
存储库层
public class ClarityResourceAffectationRepositoryImpl implements ClarityResourceAffectationRepositoryCustom {
@Autowired
MongoTemplate mongoTemplate;
@Override
public List<ClarityResourceAffectationReport> aggregateAllTaskCode() {
AggregationOperation project = new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
return new BasicDBObject("$project", new BasicDBObject("budget_code", "$budget_code").append("task_code", Arrays.asList("$task_code")));
}
};
Aggregation aggregation = newAggregation(project,
group("budgetCode").addToSet("budgetCode").as("budgetCode").addToSet("taskCode").as("taskCode"),
sort(Sort.Direction.ASC, previousOperation(),"budgetCode"));
AggregationResults groupResults = mongoTemplate.aggregate(aggregation, ClarityResourceAffectation.class,
ClarityResourceAffectationReport.class);
List<ClarityResourceAffectationReport> clarityResourceAffectationReports = groupResults.getMappedResults();
return clarityResourceAffectationReports;
}
}
public class ClarityResourceAffectationServiceImpl implements ClarityResourceAffectationService{
@Override
public List<ClarityResourceAffectationReport> aggregateAllTaskCodes() {
log.debug("Request to aggregateAllTaskCodes: {}");
List<ClarityResourceAffectationReport> result = clarityResourceAffectationRepository
.aggregateAllTaskCodes();
return result;
}
}
public class ClarityResourceAffectationResource {
@GetMapping("/clarity-resource-affectations/list-task-codes")
@Timed
public ResponseEntity<List<ClarityResourceAffectationReport>> aggregateTabAllTaskCodes() {
log.debug("REST request to get aggregateTabAllTaskCodes : {}");
List<ClarityResourceAffectationReport> result = clarityResourceAffectationService.aggregateAllTaskCodes();
return new ResponseEntity<>(result, HttpStatus.OK);
}
}
清晰资源影响报告
public class ClarityResourceAffectationReport implements Serializable {
private static final long serialVersionUID = 1L;
private String[] budgetCodes;
private String[][] taskCodes;
private String[][][] actionCodes;
public String[] getBudgetCodes() {
return budgetCodes;
}
public void setBudgetCodes(String[] budgetCodes) {
this.budgetCodes = budgetCodes;
}
public String[][] getTaskCodes() {
return taskCodes;
}
public void setTaskCodes(String[][] taskCodes) {
this.taskCodes = taskCodes;
}
public String[][][] getActionCodes() {
return actionCodes;
}
public void setActionCodes(String[][][] actionCodes) {
this.actionCodes = actionCodes;
}
}
我在数据库中拥有的示例
/* 0 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "61427",
"action_code": "354"
}
/* 1 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "61427",
"action_code": "121"
}
/* 2 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "65434",
"action_code": "143"
}
/* 3 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "65434",
"action_code": "463"
}
/* 4 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "60298",
"action_code": "255"
}
/* 5 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "60298",
"action_code": "127"
}
/* 6 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "67875",
"action_code": "348"
}
/* 7 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "67875",
"action_code": "654"
}
public class ClarityResourceAffectationReport {
private String budgetCode;
private List<TaskCode> linkedTaskCodes;
}
public class TaskCode {
private String taskCode;
private String[] actionCode;
}
Aggregation aggregation = newAggregation(
group("budgetCode", "taskCode").addToSet("actionCode").as("actionCode"),
group("_id.budgetCode").addToSet(new BasicDBObject("taskCode","$_id.taskCode").append("actionCode", "$actionCode")).as("linkedTaskCodes"),
project("linkedTaskCodes").and("budgetCode").previousOperation().andExclude("_id"),
sort(Sort.Direction.ASC, "budgetCode"));
目前,我有一个预算代码[“P221P00”]
,一个链接任务代码列表[[“2630”],[“61297”],[“61296”],[“61299”]
。我想在CLARITYRESOURCEAffectionReport文件String[][]actionCode
中添加第三个字段,以便进行另一级别的分组。通过这种方式,任务代码
列表的每个元素都将链接到操作代码
列表。在另一个Stackoverflow帖子中,Veeram建议我做两组,一组在budgetCode
和taskCodes
上推actionCode
,另一组在budgetCode
上推上一组的taskCodes
和actionCode
因此,我开发了一个包含两个项目操作和两个聚合操作的方法。我不知道是不是这样。另外,我不知道如何形成组结果,因为现在我们有两个聚合操作。此外,我认为现在在ClarityResourceAffectationReport中,我有三个数组:<代码> String []预算代码,String [][TaskCu码,String []][ActhOnguls< /C> > /<
@Override
public List<ClarityResourceAffectationReport> aggregateAllBudgetCode() {
//I did two projects instantiations
AggregationOperation projectBudgetTask = new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
return new BasicDBObject("$project", new BasicDBObject("budget_code", "$budget_code").append("task_code", Arrays.asList("$task_code")));
}
};
AggregationOperation projectTaskAction = new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
return new BasicDBObject("$project", new BasicDBObject("task_code", "$task_code").append("action_code", Arrays.asList("$action_code")));
}
};
//I did two aggregation methods
Aggregation aggregationBudgetTask = newAggregation(projectBudgetTask,
group("budgetCode", "taskCode").addToSet("budgetCodeTaskCode").as("budgetCodeTaskCode").addToSet("actionCode").as("actionCode"),
sort(Sort.Direction.ASC, previousOperation(),"budgetCode", "taskCode"));
Aggregation aggregationTaskAction = newAggregation(projectTaskAction,
group("budgetCode").addToSet("budgetCode").as("budgetCode").addToSet("taskCodes").as("taskCodes").addToSet("actionCode").as("actionCode"),
sort(Sort.Direction.ASC, previousOperation(),"budgetCode"));
//Here, how can I put the two aggregation methods?
AggregationResults groupResults = mongoTemplate.aggregate(aggregation, clarityResourceAffectation.class,
ClarityResourceAffectationReport.class);
List<ClarityResourceAffectationReport> clarityResourceAffectationReport = groupResults.getMappedResults();
return clarityResourceAffectationReport ;
}
我没有运行,我有一个错误:f.b.k.l.w.r.errors.ExceptionTranslator:targetbean的类型[[Ljava.lang.String;不是持久实体的类型([Ljava.lang.String;)!
。无论如何,在代码中,您也将.append(“actionCodes”,“actionCodes”)。作为(“taskCodeActionCodes”)
在第二组之后。您能解释一下什么是taskCodeActionCodes
结构平坦的结果
/* 0 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "61427",
"action_code": "354"
}
/* 1 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "61427",
"action_code": "121"
}
/* 2 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "65434",
"action_code": "143"
}
/* 3 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "65434",
"action_code": "463"
}
/* 4 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "60298",
"action_code": "255"
}
/* 5 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "60298",
"action_code": "127"
}
/* 6 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "67875",
"action_code": "348"
}
/* 7 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "67875",
"action_code": "654"
}
public class ClarityResourceAffectationReport {
private String budgetCode;
private List<TaskCode> linkedTaskCodes;
}
public class TaskCode {
private String taskCode;
private String[] actionCode;
}
Aggregation aggregation = newAggregation(
group("budgetCode", "taskCode").addToSet("actionCode").as("actionCode"),
group("_id.budgetCode").addToSet(new BasicDBObject("taskCode","$_id.taskCode").append("actionCode", "$actionCode")).as("linkedTaskCodes"),
project("linkedTaskCodes").and("budgetCode").previousOperation().andExclude("_id"),
sort(Sort.Direction.ASC, "budgetCode"));
我稍微更改了ResourceAffectationReport
字段的类型:String budgetCodes、String[]taskCodes和String[]actionCodes
[
{
"budgetCodes": "P24D001",
"taskCodes": [
"64578"
],
"actionCodes": [
[
"454"
],
[
"253"
],
[
"745"
],
[
"354"
]
]
},
{
"budgetCodes": "P24D002",
"taskCodes": [
"62678"
],
"actionCodes": [
[
"857"
],
[
"907"
],
[
"858"
]
]
}
]
[
{
"budgetCodes": "P24D001",
"taskCodes": null,
"actionCodes": null,
"taskCodesactionCodes": [
{
"[Ljava.lang.String;@7e695137": [
[
"64578"
]
],
"[Ljava.lang.String;@48ec311a": [
[
"454"
],
[
"253"
],
[
"745"
],
[
"354"
]
]
},
{
"[Ljava.lang.String;@258b30b6": [
[
"62678"
]
],
"[Ljava.lang.String;@481154c7": [
[
"857"
],
[
"907"
],
[
"858"
]
}
]
},
{
"budgetCodes": "P24D002",
"taskCodes": null,
"actionCodes": null,
"taskCodesActionCodes": [
{
"[Ljava.lang.String;@1cdf4a9e": [
[
"64568"
]
],
"[Ljava.lang.String;@1613fdbb": [
[
"764"
],
[
"984"
],
[
"489"
]
]
},
{
"[Ljava.lang.String;@53167f62": [
[
"63887"
]
],
"[Ljava.lang.String;@5a30c8de": [
[
"757"
],
[
"394"
],
[
"294"
],
[
"765"
]
]
}
]
}
]
具有映射结构的结果
/* 0 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "61427",
"action_code": "354"
}
/* 1 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "61427",
"action_code": "121"
}
/* 2 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "65434",
"action_code": "143"
}
/* 3 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "65434",
"action_code": "463"
}
/* 4 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "60298",
"action_code": "255"
}
/* 5 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "60298",
"action_code": "127"
}
/* 6 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "67875",
"action_code": "348"
}
/* 7 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "67875",
"action_code": "654"
}
public class ClarityResourceAffectationReport {
private String budgetCode;
private List<TaskCode> linkedTaskCodes;
}
public class TaskCode {
private String taskCode;
private String[] actionCode;
}
Aggregation aggregation = newAggregation(
group("budgetCode", "taskCode").addToSet("actionCode").as("actionCode"),
group("_id.budgetCode").addToSet(new BasicDBObject("taskCode","$_id.taskCode").append("actionCode", "$actionCode")).as("linkedTaskCodes"),
project("linkedTaskCodes").and("budgetCode").previousOperation().andExclude("_id"),
sort(Sort.Direction.ASC, "budgetCode"));
这里是报告文件字符串budgetCode、字符串[]taskCodes、字符串[][]actionCodes、列表>taskCodesActionCodes的类型
[
{
"budgetCodes": "P24D001",
"taskCodes": [
"64578"
],
"actionCodes": [
[
"454"
],
[
"253"
],
[
"745"
],
[
"354"
]
]
},
{
"budgetCodes": "P24D002",
"taskCodes": [
"62678"
],
"actionCodes": [
[
"857"
],
[
"907"
],
[
"858"
]
]
}
]
[
{
"budgetCodes": "P24D001",
"taskCodes": null,
"actionCodes": null,
"taskCodesactionCodes": [
{
"[Ljava.lang.String;@7e695137": [
[
"64578"
]
],
"[Ljava.lang.String;@48ec311a": [
[
"454"
],
[
"253"
],
[
"745"
],
[
"354"
]
]
},
{
"[Ljava.lang.String;@258b30b6": [
[
"62678"
]
],
"[Ljava.lang.String;@481154c7": [
[
"857"
],
[
"907"
],
[
"858"
]
}
]
},
{
"budgetCodes": "P24D002",
"taskCodes": null,
"actionCodes": null,
"taskCodesActionCodes": [
{
"[Ljava.lang.String;@1cdf4a9e": [
[
"64568"
]
],
"[Ljava.lang.String;@1613fdbb": [
[
"764"
],
[
"984"
],
[
"489"
]
]
},
{
"[Ljava.lang.String;@53167f62": [
[
"63887"
]
],
"[Ljava.lang.String;@5a30c8de": [
[
"757"
],
[
"394"
],
[
"294"
],
[
"765"
]
]
}
]
}
]
map structure方法很有趣,但为了在前端显示它,我遇到了一些问题,但这是另一个问题,因为键是对象类型的。我会再次测试,我会告诉你。无论如何,它是相互测试的。实际上,我有另一个想法。我想要的是,在每个JSON文档中,我都有一个字符串字段budget代码,一个字符串[]字段任务代码,将包含所有
taskCodes链接到budgetCodes,我认为最终真正适合我需要的是创建这样一个TaskCode类:
public class TaskCode {
private String taskCode;
private String[] actionCode;
//Getters and Setters
}
这样,我就可以为每个JSON文档提供一个简单的字符串和一个budgetCode,以及一个带有for的链接任务代码列表
每个taskCode对象,字符串属性taskCode,它将包含taskCode的值。最后,每个taskCode对象将包含字符串[]
与行动代码列表
[
{
"budgetCodes": "P24D001",
"taskCodes": [
"64578"
],
"actionCodes": [
[
"454"
],
[
"253"
],
[
"745"
],
[
"354"
]
]
},
{
"budgetCodes": "P24D002",
"taskCodes": [
"62678"
],
"actionCodes": [
[
"857"
],
[
"907"
],
[
"858"
]
]
}
]
[
{
"budgetCodes": "P24D001",
"taskCodes": null,
"actionCodes": null,
"taskCodesactionCodes": [
{
"[Ljava.lang.String;@7e695137": [
[
"64578"
]
],
"[Ljava.lang.String;@48ec311a": [
[
"454"
],
[
"253"
],
[
"745"
],
[
"354"
]
]
},
{
"[Ljava.lang.String;@258b30b6": [
[
"62678"
]
],
"[Ljava.lang.String;@481154c7": [
[
"857"
],
[
"907"
],
[
"858"
]
}
]
},
{
"budgetCodes": "P24D002",
"taskCodes": null,
"actionCodes": null,
"taskCodesActionCodes": [
{
"[Ljava.lang.String;@1cdf4a9e": [
[
"64568"
]
],
"[Ljava.lang.String;@1613fdbb": [
[
"764"
],
[
"984"
],
[
"489"
]
]
},
{
"[Ljava.lang.String;@53167f62": [
[
"63887"
]
],
"[Ljava.lang.String;@5a30c8de": [
[
"757"
],
[
"394"
],
[
"294"
],
[
"765"
]
]
}
]
}
]
关于POJO结构
/* 0 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "61427",
"action_code": "354"
}
/* 1 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "61427",
"action_code": "121"
}
/* 2 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "65434",
"action_code": "143"
}
/* 3 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "65434",
"action_code": "463"
}
/* 4 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "60298",
"action_code": "255"
}
/* 5 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "60298",
"action_code": "127"
}
/* 6 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "67875",
"action_code": "348"
}
/* 7 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "67875",
"action_code": "654"
}
public class ClarityResourceAffectationReport {
private String budgetCode;
private List<TaskCode> linkedTaskCodes;
}
public class TaskCode {
private String taskCode;
private String[] actionCode;
}
Aggregation aggregation = newAggregation(
group("budgetCode", "taskCode").addToSet("actionCode").as("actionCode"),
group("_id.budgetCode").addToSet(new BasicDBObject("taskCode","$_id.taskCode").append("actionCode", "$actionCode")).as("linkedTaskCodes"),
project("linkedTaskCodes").and("budgetCode").previousOperation().andExclude("_id"),
sort(Sort.Direction.ASC, "budgetCode"));
它的功能很好,但是为了在AngularJS前端显示数据,我遇到了一些问题。我想做一个聚合操作,以便创建一个菜单,在这个菜单中,每次单击一个元素都会打开一个带有子列表的子菜单等等……我不知道聚合是否真的适合我想要做的事情。所以,我想t关于另一种方法。不做聚合操作,也许另一种方法可以是通过简单的查找操作从数据库中检索我想要的数据,并使用服务层执行树算法
提前感谢您可以在单个聚合管道中执行多个级别分组 更新现有项目阶段以包括
actionCode
,并更新现有组以包括actionCode
这将显示扁平结构,每行包含预算代码
、任务代码
和操作代码
@Override
public List<ClarityResourceAffectationReport> aggregateAllBudgetCode() {
//I did two projects instantiations
AggregationOperation projectBudgetTask = new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
return new BasicDBObject("$project", new BasicDBObject("budget_code", "$budget_code").append("task_code", Arrays.asList("$task_code")));
}
};
AggregationOperation projectTaskAction = new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
return new BasicDBObject("$project", new BasicDBObject("task_code", "$task_code").append("action_code", Arrays.asList("$action_code")));
}
};
//I did two aggregation methods
Aggregation aggregationBudgetTask = newAggregation(projectBudgetTask,
group("budgetCode", "taskCode").addToSet("budgetCodeTaskCode").as("budgetCodeTaskCode").addToSet("actionCode").as("actionCode"),
sort(Sort.Direction.ASC, previousOperation(),"budgetCode", "taskCode"));
Aggregation aggregationTaskAction = newAggregation(projectTaskAction,
group("budgetCode").addToSet("budgetCode").as("budgetCode").addToSet("taskCodes").as("taskCodes").addToSet("actionCode").as("actionCode"),
sort(Sort.Direction.ASC, previousOperation(),"budgetCode"));
//Here, how can I put the two aggregation methods?
AggregationResults groupResults = mongoTemplate.aggregate(aggregation, clarityResourceAffectation.class,
ClarityResourceAffectationReport.class);
List<ClarityResourceAffectationReport> clarityResourceAffectationReport = groupResults.getMappedResults();
return clarityResourceAffectationReport ;
}
将您的actionCodes
更新为private String[][]actionCodes;
扁平结构。
差不多
AggregationOperation project = new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
return new BasicDBObject("$project", new BasicDBObject("budget_code", 1).append("task_code", Arrays.asList("$task_code")).append("action_code", Arrays.asList("$action_code")));
}
};
Aggregation aggregation = newAggregation(project,
group("budgetCode", "taskCode").addToSet("actionCode").as("actionCodes"),
project("actionCodes").and("_id.budgetCode").as("budgetCodes").and("_id.taskCode").as("taskCodes").andExclude("_id"),
sort(Sort.Direction.ASC, "budgetCodes","taskCodes"));
AggregationOperation project = new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
return new BasicDBObject("$project", new BasicDBObject("budget_code", 1).append("task_code", Arrays.asList("$task_code")).append("action_code", Arrays.asList("$action_code")));
}
};
Aggregation aggregation = newAggregation(project,
group("budgetCode", "taskCode").addToSet("actionCode").as("actionCodes"),
group("_id.budgetCode").addToSet(new BasicDBObject("taskCode","$_id.taskCode").append("actionCodes", "$actionCodes")).as("taskCodeActionCodes"),
project("taskCodeActionCodes").and("budgetCodes").previousOperation().andExclude("_id"),
sort(Sort.Direction.ASC, "budgetCodes"));
类似地图的结构。
将输出DTO调整为
private String[] budgetCodes;
private List<Map<String[], String[][]>> taskCodeActionCodes;
POJO结构
/* 0 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "61427",
"action_code": "354"
}
/* 1 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "61427",
"action_code": "121"
}
/* 2 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "65434",
"action_code": "143"
}
/* 3 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24DCDSA01",
"task_code": "65434",
"action_code": "463"
}
/* 4 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "60298",
"action_code": "255"
}
/* 5 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "60298",
"action_code": "127"
}
/* 6 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "67875",
"action_code": "348"
}
/* 7 */
{
"_class": "fr.bpce.kpi.clarity.domain.ClarityResourceAffectation",
"budget_code": "P24PR00",
"task_code": "67875",
"action_code": "654"
}
public class ClarityResourceAffectationReport {
private String budgetCode;
private List<TaskCode> linkedTaskCodes;
}
public class TaskCode {
private String taskCode;
private String[] actionCode;
}
Aggregation aggregation = newAggregation(
group("budgetCode", "taskCode").addToSet("actionCode").as("actionCode"),
group("_id.budgetCode").addToSet(new BasicDBObject("taskCode","$_id.taskCode").append("actionCode", "$actionCode")).as("linkedTaskCodes"),
project("linkedTaskCodes").and("budgetCode").previousOperation().andExclude("_id"),
sort(Sort.Direction.ASC, "budgetCode"));
公共类ClarityresourceEffection报告{
私有字符串预算代码;
私有列表链接的taskcode;
}
公共类任务代码{
私有字符串任务码;
私有字符串[]操作码;
}
聚合=新聚合(
集团(“预算代码”、“任务代码”).addToSet(“操作代码”).as(“操作代码”),
组(“\u id.budgetCode”).addToSet(新的BasicDBObject(“taskCode”),“$\u id.taskCode”).append(“actionCode”,“$actionCode”))。作为(“linkedTaskCodes”),
项目(“linkedTaskCodes”)和(“budgetCode”).previousOperation()和Exclude(“\u id”),
排序(sort.Direction.ASC,“预算代码”);
您可以在单个聚合管道中执行多个级别分组
更新现有项目阶段以包括actionCode
,并更新现有组以包括actionCode
这将显示扁平结构,每行包含预算代码
、任务代码
和操作代码
@Override
public List<ClarityResourceAffectationReport> aggregateAllBudgetCode() {
//I did two projects instantiations
AggregationOperation projectBudgetTask = new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
return new BasicDBObject("$project", new BasicDBObject("budget_code", "$budget_code").append("task_code", Arrays.asList("$task_code")));
}
};
AggregationOperation projectTaskAction = new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
return new BasicDBObject("$project", new BasicDBObject("task_code", "$task_code").append("action_code", Arrays.asList("$action_code")));
}
};
//I did two aggregation methods
Aggregation aggregationBudgetTask = newAggregation(projectBudgetTask,
group("budgetCode", "taskCode").addToSet("budgetCodeTaskCode").as("budgetCodeTaskCode").addToSet("actionCode").as("actionCode"),
sort(Sort.Direction.ASC, previousOperation(),"budgetCode", "taskCode"));
Aggregation aggregationTaskAction = newAggregation(projectTaskAction,
group("budgetCode").addToSet("budgetCode").as("budgetCode").addToSet("taskCodes").as("taskCodes").addToSet("actionCode").as("actionCode"),
sort(Sort.Direction.ASC, previousOperation(),"budgetCode"));
//Here, how can I put the two aggregation methods?
AggregationResults groupResults = mongoTemplate.aggregate(aggregation, clarityResourceAffectation.class,
ClarityResourceAffectationReport.class);
List<ClarityResourceAffectationReport> clarityResourceAffectationReport = groupResults.getMappedResults();
return clarityResourceAffectationReport ;
}
将您的actionCodes
更新为p