gpt4 book ai didi

java - Spring Data + Mongo- 无分组字段求和

转载 作者:可可西里 更新时间:2023-11-01 10:02:52 25 4
gpt4 key购买 nike

我有这样的匹配标准-

Criteria criteria = new Criteria()
.and("paidMobileMetadata").in(metadataList)
.and("localDate").gt(startDate).lte(endDate);

现在我想得到字段的总和,说“钱”属于上述标准,而不对某些字段进行分组。

早些时候我遇到了同样的问题,我必须在某个字段上对这个匹配标准进行分组,我是这样做的-

Aggregation agg = Aggregation.newAggregation(
Aggregation.match(new Criteria()
.and("paidMobileMetadata").in(metadataList)
.and("localDate").gt(startDate).lte(endDate)),
Aggregation.group("anyField").sum("money").as("total")
);

但是这里我不能对它进行分组,mongo中有什么方法可以对所有文档进行分组,而无需任何字段。

Explaination: let say 500 row/doc belonging to my criteria and all have money field in it.I want to sum of all 500 money,without grouping.

当我这样尝试时-

Aggregation agg = Aggregation.newAggregation(
Aggregation.match(new Criteria()
.and("paidMobileMetadata").in(metadataList)
.and("localDate").gt(startDate).lte(endDate)),
Aggregation.group(null).sum("money").as("total")
);

给我异常说聚合字段不能为 null 或为空!

最佳答案

只需不带参数传入,因为不提供一个等于 null:

Aggregation agg = Aggregation.newAggregation(
Aggregation.match(new Criteria()
.and("paidMobileMetadata").in(metadataList)
.and("localDate").gt(startDate).lte(endDate)),
Aggregation.group().sum("money").as("total")
);

关于java - Spring Data + Mongo- 无分组字段求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38554053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com