汇总所有文档的字段值

时间:2019-02-04 14:05:44

标签: java mongodb

我想对mongoDB中所有文档的字段值求和,因此尝试使用具有空字段的$ group,然后尝试$sum。它提供了预期的结果,但是当我同样尝试使用mongoOperation在Java中进行转换时,出现错误,因为组值不能为null或为空。

以下是我的收藏集中的数据:

{ "_id" : ObjectId("5c583ee7c1299106eaeec6bf"), "_class" : "com.health", "startTime" : ISODate("2019-02-04T13:02:00Z"), "endTime" : ISODate("2019-02-04T13:32:23.958Z"), "agentStatsList" : [ { "Name" : "ABC", "totalRefresh" : 103, "success" : 87, "failure" : 16 }, { "Name" : "PQR", "totalRefresh" : 19, "success" : 10, "failure" : 9 }, { "Name" : "XYZ", "totalRefresh" : 14, "success" : 13, "failure" : 1 }]}

UnwindOperation unwind=Aggregation.unwind("agentStatsList");
MatchOperation match=Aggregation.match(Criteria.where("agentStatsList.agentName").in(listOfAgents).
            and("startTime").lte(new Date(System.currentTimeMillis() - 3600 * 1000)));      
GroupOperation group=Aggregation.group("").sum("agentStatsList.success").as("success").sum("agentStatsList.failure").as("failure").
sum("agentStatsList.totalRefresh").as("totalRefresh");
Aggregation aggregation=Aggregation.newAggregation(unwind,match,group);

Shell Query工作正常,但在Java中却无法正常工作。

0 个答案:

没有答案
相关问题