26
26
import org .springframework .data .domain .Sort .Direction ;
27
27
import org .springframework .data .mongodb .core .aggregation .ExposedFields .ExposedField ;
28
28
import org .springframework .data .mongodb .core .aggregation .ExposedFields .FieldReference ;
29
- import org .springframework .data .mongodb .core .aggregation .Fields .AggregationField ;
29
+ import org .springframework .data .mongodb .core .aggregation .Fields .* ;
30
30
import org .springframework .data .mongodb .core .aggregation .FieldsExposingAggregationOperation .InheritsFieldsAggregationOperation ;
31
31
import org .springframework .data .mongodb .core .query .Criteria ;
32
32
import org .springframework .data .mongodb .core .query .NearQuery ;
@@ -159,9 +159,9 @@ protected Aggregation(List<AggregationOperation> aggregationOperations, Aggregat
159
159
Assert .isTrue (!aggregationOperations .isEmpty (), "At least one AggregationOperation has to be provided" );
160
160
Assert .notNull (options , "AggregationOptions must not be null!" );
161
161
162
- //check $out is the last operation if exist
163
- for (int i = 0 ; i < aggregationOperations . size (); i ++ ) {
164
- if (aggregationOperations . get ( i ) instanceof OutOperation && i != aggregationOperations . size () - 1 ) {
162
+ // check $out is the last operation if it exists
163
+ for (AggregationOperation aggregationOperation : aggregationOperations ) {
164
+ if (aggregationOperation instanceof OutOperation && ! isLast ( aggregationOperation , aggregationOperations ) ) {
165
165
throw new IllegalArgumentException ("The $out operator must be the last stage in the pipeline." );
166
166
}
167
167
}
@@ -170,6 +170,10 @@ protected Aggregation(List<AggregationOperation> aggregationOperations, Aggregat
170
170
this .options = options ;
171
171
}
172
172
173
+ private boolean isLast (AggregationOperation aggregationOperation , List <AggregationOperation > aggregationOperations ) {
174
+ return aggregationOperations .indexOf (aggregationOperation ) == aggregationOperations .size () - 1 ;
175
+ }
176
+
173
177
/**
174
178
* A pointer to the previous {@link AggregationOperation}.
175
179
*
0 commit comments