MongoTemplate中的Groupby返回空字段

我有一个领域的集合id, a-int, b-int, c-int, total-int。我试图获取,a, b, c,

total但最终得到的只是total字段值的总和,其余为0, 0, 0。我该如何解决?以下数据样本的预期结果10, 20, 30, 300

谢谢

数据样本

id,   a,  b,  c, total

xid, 10, 20, 30, 100

xid, 10, 20, 30, 200

GroupBy groupBy = GroupBy.key("{a : 1, b : 1, c : 1}")

.initialDocument("{ total: 0 }")

.reduceFunction("function(obj, result) { result.total += obj.total; }");

GroupByResults<Grouped> results = mongoTemplate.group(Criteria.where("id").is(id),

TABLE, groupBy, Grouped.class);

回答:

我得到了您认为想要使用以下结果的结果:

GroupBy groupBy = GroupBy.key("a", "b", "c")

.initialDocument("{ total: 0 }")

.reduceFunction("function(obj, result) { " +

" result.a = obj.a; " +

" result.b = obj.b; " +

" result.c = obj.c; " +

" result.total += obj.total; " +

"}");

请注意,您需要做的是告诉reduce函数将哪些内容放入a,b和c字段以及总字段中。

这给了我原始输出:

{ "a" : 10.0 , "b" : 20.0 , "c" : 30.0 , "total" : 300.0}

由于您尚未包含Grouped类,因此我不确定它是否完全映射到您想要的对象中,但是它可能会为您指明正确的方向。

以上是 MongoTemplate中的Groupby返回空字段 的全部内容, 来源链接: utcz.com/qa/412724.html

回到顶部