首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >弹簧数据MongoDB不存在开关错误

弹簧数据MongoDB不存在开关错误
EN

Stack Overflow用户
提问于 2020-09-17 06:46:49
回答 2查看 248关注 0票数 0

我正在使用MongoDB运行春季服务背后的aggreagaton逻辑。

聚合逻辑如下所示:

代码语言:javascript
运行
复制
MatchOperation dateMatchOperation = Aggregation.match(
    Criteria.
            where("date").
            gte(new Date(startStamp)).lte(new Date(endStamp)));

MatchOperation propertyMatchOperation = Aggregation.match(
    Criteria.
            where("abc1").is(abcVal1)
            .and("abc2").is(abcVal2)
            .and("abc3").is(abcVal3)
            .and("abc4").is(abcVal4)
            .and("abc5").is(abcVal5)
);

List<Date> dates = new ArrayList<>();
//Create appropriate interval arraylist which will be passed to mongo
for (int i = 0; i < (endStamp - startStamp)/aggregationInterval; i ++) {
dates.add(new Date(startStamp + i * aggregationInterval));
}


BucketOperation bucketOperation = Aggregation.bucket("date").withBoundaries(dates.toArray())
    .andOutput(AccumulatorOperators.Sum.sumOf(aggregationInput)).as("value")
    .andOutput(AccumulatorOperators.Min.minOf("date")).as("from")
    .andOutput(AccumulatorOperators.Max.maxOf("date")).as("to");


AggregationOptions aggregationOptions = AggregationOptions.builder().allowDiskUse(true).build();
AggregationResults<MetricAggregationResult> aggregationResults = mongoTemplate.aggregate(
    Aggregation.newAggregation(dateMatchOperation, propertyMatchOperation, bucketOperation)
        .withOptions(aggregationOptions),
    "mongocollectionname",
    MetricAggregationResult.class);

我正在用450万份文件在集合上测试这个。当aggregationInterval很小且数组中有许多元素时,它可以工作,但是在某个点上,逐渐增加聚合间隔,我注意到在某些点聚集抛出之后,会出现以下错误:

代码语言:javascript
运行
复制
com.mongodb.MongoCommandException: Command failed with error 40066: '$switch could not find a matching branch for an input, and no default was specified.'

这很奇怪,因为我的逻辑中没有使用任何$switch聚合,至于AggregationOptions,我认为mongo聚合达到了100‘s的极限和我允许磁盘使用。

在这一点上,我的手被绑住了,我不知道是什么引起了问题(我在StackOverflow上到处搜索$switch错误,但是我找不到任何东西,因为所有询问的人都在一定程度上使用了$switch ),但我很有信心这是蒙戈一方错过的东西。

EN

回答 2

Stack Overflow用户

发布于 2020-10-21 17:46:30

代码语言:javascript
运行
复制
Use BucketAutoOperation

 BucketAutoOperation bucketAutoOperation = Aggregation.bucketAuto("date", 2)
                .andOutput(AccumulatorOperators.Sum.sumOf(aggregationInput)).as("value")
                .andOutput(AccumulatorOperators.Min.minOf("date")).as("from")
                .andOutput(AccumulatorOperators.Max.maxOf("date")).as("to");
票数 0
EN

Stack Overflow用户

发布于 2020-10-23 09:41:13

代码语言:javascript
运行
复制
To use a bucket operation, we specify the following:

groupBy: the field that the boundaries will apply to. This field must be numeric or a date field.
boundaries: an array of boundary points. Documents which have a groupBy field falling between two elements in the array go into that bucket. The between test here is half-open, so the first point is inclusive, second point is exclusive, which is the behavior developers would expect
default: any documents in the pipeline which don’t go into one of the buckets will go into default. This is required. Using a match operation in the pipeline before the bucket operation will remove documents which shouldn’t be processed.
output: an aggregation expression to generate the output document for each bucket


IN the above code, we missed specifying default. 

final BucketOperation bucketOperation = Aggregation.bucket("date").
                withBoundaries(now.minus(10, ChronoUnit.DAYS), now.minus(9, DAYS),
                        now.minus(8, DAYS), now.minus(7, DAYS), now.minus(6, DAYS),
                        now.minus(5, DAYS), now.minus(4, DAYS), now.minus(3, DAYS),
                        now.minus(2, DAYS), now.minus(1, DAYS), now.minus(0, DAYS)).
                withDefaultBucket("defaultBucket").
                andOutput(countingExpression).as("count");

请参阅以下链接:- https://chiralsoftware.com/idea/spring-data-aggregation-operations

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63932637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档