Spring Boot聚合未得到预期的结果

时间:2018-08-01 14:19:32

标签: java spring-boot grouping

我在进行汇总时遇到问题。这是我的json:

{
"criteria": {
    "partyType": "Person",
    "facets": [
        "branchId", "partType"....
        ]
}

}

匹配条件为“ partyType”:“人”,我想汇总一个或多个字段:

"facets": [
        "branchId", "partType"....
        ]

这是我的BaseDAO代码:

private <T> List<T> getAggregateResults(List<String> facets)
{
    String strFacets = StringUtils.join(facets, ',');
    Aggregation agg = newAggregation(
        match(Criteria.where("_id.partyType").is("Person")), 
        project(strFacets).and("totalCount"),
        group(strFacets).count().as("totalCount"))
            .withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build());

    AggregationResults<T> groupResults = (AggregationResults<T>) mongoTemplate.aggregate(
        agg,
        this.getCollectionName(), FacetResults.class);

    return groupResults.getMappedResults();
}

对“ branchId”执行分组时,输出如下所示:

"aggResults"     (id=157)   
[0...99]    
    [0] FacetResults  (id=168)  
    [1] FacetResults  (id=169)  
        Id  null    
        totalCount  8   
    [2] FacetResults  (id=170)  

如果您注意到Id为空,则它应该是分支Id。如果我对2个字段进行分组,则输出中的i项目为: ID空
总数1830000

我的facetResults类如下:

public class FacetResults

    {
    @Getter
    @Setter
    private String Id;
    @Getter
    @Setter
    private long totalCount;
}

请帮助我了解我在做什么错?

0 个答案:

没有答案