Mongo - exception aggregation result exceeds maximum document size 16MB

May 31, 2016

Problem:

exception: aggregation result exceeds maximum document size (16MB)

Solution:

Configure the aggregation options and set it to return the results as a cursor:<![CDATA[ public BasicDBList aggregateWithCursor(Aggregation aggregation, Object source, Object target) { aggregation = aggregation.withOptions(Aggregation.newAggregationOptions().cursor(new BasicDBObject()).allowDiskUse(true).build()); AggregationResults<? extends Object> aggregationResults = mongoAccess.getMongoTemplate().aggregate(aggregation, source.getClass(), target.getClass()); BasicDBObject cursor = ((BasicDBObject) aggregationResults.getRawResults().get(“cursor”)); return (BasicDBList) cursor.get(“firstBatch”); } ]]>
SyntaxHighlighter.highlight();