'How to add "allowDiskUse" in @Aggregation annotation in repository in Spring Boot?

I have an aggregation written in a MyRepository.kt file which is being called from MongoDataRetriever.kt file in the backend.

MyRepository.kt file:

import org.springframework.data.mongodb.repository.Aggregation
import org.springframework.data.mongodb.repository.MongoRepository

  @Aggregation(pipeline = [
    "{ \$match: { 'objName' : { \$exists: true } } }",
    "{ \$sort: { 'addedDate': -1 } }"
  ])
  fun getLatestObjectsWithLatestData(): List<MyDocument>

and MongoDataRetriever.kt file:

  override fun getLatestObjects(): List<MyObj> {
    return myRepository.getLatestObjectsWithLatestData().map { it.toMyObj() }
  }

The above aggregation is failing with error:

message: "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 serv...

So, It seems adding allowDiskUse = true or something like that is the possible fix, but how to add that in the above annotation?



Solution 1:[1]

You can add @Meta(allowDiskUse = true) annotation to your method.

@Aggregation(pipeline = [
"{ \$match: { 'objName' : { \$exists: true } } }",
"{ \$sort: { 'addedDate': -1 } }"
])
@Meta(allowDiskUse = true)
fun getLatestObjectsWithLatestData(): List<MyDocument>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Light Yagami