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