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.