Spring Mongo> How to get AggregationOperations list from aggregation

I have a function that receives Aggregation aggregation as a parameter.

I would like to get all AggregationOperation from aggregation . Is there any way to do this?

 public Aggregation newCustomAggregation(Aggregation aggregation, Criteria c) { // How to get list operation aggregation there? listOperation.push(Aggregation.match(c)); return Aggregation .newAggregation(listOperations); } 

My goal is a new new aggregation with my custom MatchAggregation .

+7
java spring-data-mongodb mongodb spring-mongodb
source share
3 answers

You can create your own aggregation implementation by subclassing aggregation to access the protected operations field.

Something like

 public class CustomAggregation extends Aggregation { List<AggregationOperation> getAggregationOperations() { return operations; } } public Aggregation newCustomAggregation(Aggregation aggregation, Criteria c) { CustomAggregation customAggregation = (CustomAggregation) aggregation; List<AggregationOperation> listOperations = customAggregation.getAggregationOperations(); listOperations.add(Aggregation.match(c)); return Aggregation .newAggregation(listOperations); } 
+4
source share

Short answer: No, there is no good way to do this.

There is no β€œeasy” way to get the AggregationOperation list from outside the AggregationOperation instance - operations is a protected property of the Aggregation class.

You can easily get this with reflection, but such code will be fragile and expensive to maintain. There is probably a good reason for this property to remain protected. You can ask about it in Spring -MongoDB JIRA . I guess there is an alternative approach to this.

You could, of course, change your method to take the collection of the AggregationOperation parameter as a parameter, but your message also has lithium information to say if this solution is possible in your case.

+4
source share

Aggregation has an operations property that will give you the entire application operation in Aggregation .

  protected List<AggregationOperation> allOperations = aggregation.operations ; 

will provide you with all application operations.

+2
source share

All Articles