6

I'm writing a method to implement a query using MongoRepository, I'm using aggregation in a Java/Spring project, but it is giving exceeded memory limit when I'm testing.

I've tried use

newAggregationOptions().cursorBatchSize(pageable.getPageSize()).allowDiskUse(true).build()

but didn't work

my method:

...
        var matchCriteria = match(criteria);

        var unwindVariations = unwind("variations", false);


        var sort = sort(Sort.Direction.ASC, "variations.plataformStatus.status");

        var countResult = count().as("totalElements");
        var aggregationOptions = Aggregation.newAggregationOptions().cursorBatchSize(pageable.getPageSize()).allowDiskUse(true).build();

        var countAggregation = newAggregation(
                matchCriteria,
                unwindVariations,
                sort,
                countResult)
                .withOptions(aggregationOptions);

        var totalElements = mongoTemplate.aggregate(countAggregation, "PRODUCT", Map.class).getMappedResults();


error message:

com.mongodb.MongoCommandException: Command failed with error 16819 (Location16819): 'Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.' on server cluster0-shard-00-01-du380.mongodb.net:27017.
2
  • 1
    Could you identify if the Exception is thrown in the first or second aggregation operation? Commented Jun 6, 2019 at 12:56
  • I've corrected the post, only a first aggregation. Commented Jun 6, 2019 at 14:15

1 Answer 1

2

You should enable allowDiskUse option.

By default, sort operation has a 100MB limit (in memory), enabling that option in your operation will allow to use bigger size.

From MongoDB's sort documentation:

The $sort stage has a limit of 100 megabytes of RAM. By default, if the stage exceeds this limit, $sort will produce an error. To allow for the handling of large datasets, set the allowDiskUse option to true to enable $sort operations to write to temporary files.

Sign up to request clarification or add additional context in comments.

1 Comment

I've tried use newAggregationOptions().cursorBatchSize(pageable.getPageSize()).allowDiskUse(true).build() What's wrong? is already enable

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.