0

I am running aggreagaton logic behind one of my services in spring using MongoDB.

aggregation logic looks like following:

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);

I am testing this on collection with 4.5m documents. When aggregationInterval is small and there are many elements in arraylist it works OK, however at some point, gradually increasing aggregation interval I have noticed that after some point aggregation throws following error :

com.mongodb.MongoCommandException: Command failed with error 40066: '$switch could not find a matching branch for an input, and no default was specified.'

It's strange, because I am not using any $switch aggregations in my logic, as for AggregationOptions, I thought mongo aggregation was hitting it's 100MB limit and I allowed disk usage.

At this point my hands are tied, I don't know what's causing an issue (I searched all over the StackOverflow for $switch error, but I could not find anything since all asking guys had been using $switch in their code at some extent), but I'm pretty confident it's something that mongo side misses.

2 Answers 2

0
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");
1
  • From mongo's [docs.mongodb.com/manual/reference/operator/aggregation/…: "Categorizes incoming documents into a specific number of groups, called buckets, based on a specified expression. Bucket boundaries are automatically determined in an attempt to evenly distribute the documents into the specified number of buckets." Meaning that BucketAuto is not altiernative for bucket, because it doesn't have ability to provide boundaries for buckets, functionality which was mandatory in my case. Commented Oct 23, 2020 at 9:32
0
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");

Refer the below link :- https://chiralsoftware.com/idea/spring-data-aggregation-operations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.