我的文档结构如下:
{
"userId":"A",
"empId": "123",
"subId":"1",
"type":"Error"
"fieldChange":[
{
"changeFieldCategoryName":"Permanent Addr",
"changeFieldName":"Street Addr 1"
},
{
"changeFieldCategoryName":"Permanent Addr",
"changeFieldName":"Street Addr 2"
},
{
"changeFieldCategoryName":"Mailing Addr",
"changeFieldName":"Street Addr 1"
},
{
"changeFieldCategoryName":"Mailing Addr",
"changeFieldName":"Street Addr 1"
}
]
},
{
"userId":"A",
"empId": "123",
"subId":"2",
"type":"Success",
"fields":[
{
"changeFieldCategoryName":"Permanent Addr",
"changeFieldName":"Street Addr 1"
},
{
"changeFieldCategoryName":"Permanent Addr",
"changeFieldName":"Street Addr 2"
},
{
"changeFieldCategoryName":"Mailing Addr",
"changeFieldName":"Street Addr 1"
},
{
"changeFieldCategoryName":"Mailing Addr",
"changeFieldName":"Street Addr 2"
}
]
}
我需要获得输出为 “成功”和“错误”是“类型”字段。
[
{
"value of "Type field":{
"Permanent Addr": {
"Street Addr 1": 2,
"Street Addr 2": 2,
},
"Mailing Addr": {
"Street Addr 1": 2,
"Street Addr 2": 2,
}
},
"value of "Type field":{
"Permanent Addr": {
"Street Addr 1": 2,
"Street Addr 2": 2,
}
,
"Mailing Addr": {
"Street Addr 1": 2,
"Street Addr 2": 2,
}
}
]
我是聚合新手,可以,有人帮助过我,我已经尝试过 这样的人可以帮我吗,我尝试了不同的方法。没有得到 这个
Criteria criteria = new Criteria();
criteria.and("userId").is(userId);
criteria.and("subId").is(subId);
MatchOperation matchOperation = Aggregation.match(criteria);
//AggregationOperation unWindOperation = Aggregation.unwind("fieldChange", true);
GroupOperation groupOperations = Aggregation.group("fieldChange.changeFieldCategoryName",
"fieldChange.changechangeFieldName")
.count().as("errorCount")
//.addToSet("_id").as("ids")
;
//AggregationOperation projectOperation = Aggregation.project()
//.and("fieldChange.changeFieldCategoryName").as("fieldType")
//.and("fieldChange.changechangeFieldName").as("dataField");
List<AggregationOperation> operations = new ArrayList<>();
operations.add(matchOperation);
//operations.add(unWindOperation);
//operations.add(projectOperation);
operations.add(groupOperations);
Aggregation aggregation = Aggregation.newAggregation(operations);
AggregationResults results = mongoTemplate.aggregate(aggregation, Collection.class, String.class);