|
20 | 20 | import java.util.stream.Collectors;
|
21 | 21 |
|
22 | 22 | import org.bson.Document;
|
| 23 | +import org.springframework.data.domain.Pageable; |
| 24 | +import org.springframework.data.domain.SliceImpl; |
23 | 25 | import org.springframework.data.mapping.model.SpELExpressionEvaluator;
|
24 | 26 | import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
|
25 | 27 | import org.springframework.data.mongodb.core.MongoOperations;
|
@@ -76,18 +78,17 @@ public StringBasedAggregation(MongoQueryMethod method, MongoOperations mongoOper
|
76 | 78 | protected Object doExecute(MongoQueryMethod method, ResultProcessor resultProcessor,
|
77 | 79 | ConvertingParameterAccessor accessor, Class<?> typeToRead) {
|
78 | 80 |
|
79 |
| - if (method.isPageQuery() || method.isSliceQuery()) { |
80 |
| - throw new InvalidMongoDbApiUsageException(String.format( |
81 |
| - "Repository aggregation method '%s' does not support '%s' return type. Please use eg. 'List' instead.", |
82 |
| - method.getName(), method.getReturnType().getType().getSimpleName())); |
83 |
| - } |
84 |
| - |
85 | 81 | Class<?> sourceType = method.getDomainClass();
|
86 | 82 | Class<?> targetType = typeToRead;
|
87 | 83 |
|
88 | 84 | List<AggregationOperation> pipeline = computePipeline(method, accessor);
|
89 | 85 | AggregationUtils.appendSortIfPresent(pipeline, accessor, typeToRead);
|
90 |
| - AggregationUtils.appendLimitAndOffsetIfPresent(pipeline, accessor); |
| 86 | + |
| 87 | + if (method.isSliceQuery()) { |
| 88 | + AggregationUtils.appendModifiedLimitAndOffsetIfPresent(pipeline, accessor); |
| 89 | + }else{ |
| 90 | + AggregationUtils.appendLimitAndOffsetIfPresent(pipeline, accessor); |
| 91 | + } |
91 | 92 |
|
92 | 93 | boolean isSimpleReturnType = isSimpleReturnType(typeToRead);
|
93 | 94 | boolean isRawAggregationResult = ClassUtils.isAssignable(AggregationResults.class, typeToRead);
|
@@ -118,7 +119,17 @@ protected Object doExecute(MongoQueryMethod method, ResultProcessor resultProces
|
118 | 119 |
|
119 | 120 | return result.getMappedResults();
|
120 | 121 | }
|
121 |
| - |
| 122 | + |
| 123 | + List mappedResults = result.getMappedResults(); |
| 124 | + |
| 125 | + if(method.isSliceQuery()) { |
| 126 | + |
| 127 | + Pageable pageable = accessor.getPageable(); |
| 128 | + int pageSize = pageable.getPageSize(); |
| 129 | + boolean hasNext = mappedResults.size() > pageSize; |
| 130 | + return new SliceImpl<Object>(hasNext ? mappedResults.subList(0, pageSize) : mappedResults, pageable, hasNext); |
| 131 | + } |
| 132 | + |
122 | 133 | Object uniqueResult = result.getUniqueMappedResult();
|
123 | 134 |
|
124 | 135 | return isSimpleReturnType
|
|
0 commit comments