|
24 | 24 | import org.bson.Document;
|
25 | 25 | import org.springframework.data.domain.Range;
|
26 | 26 | import org.springframework.data.domain.Sort;
|
| 27 | +import org.springframework.data.domain.Sort.Direction; |
27 | 28 | import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.AsBuilder;
|
28 | 29 | import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.PropertyExpression;
|
29 | 30 | import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
|
@@ -336,6 +337,22 @@ public SortArray sort(Sort sort) {
|
336 | 337 | return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(sort);
|
337 | 338 | }
|
338 | 339 |
|
| 340 | + /** |
| 341 | + * Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given {@link Sort |
| 342 | + * order}. |
| 343 | + * |
| 344 | + * @return new instance of {@link SortArray}. |
| 345 | + * @since 4.0 |
| 346 | + */ |
| 347 | + public SortArray sort(Direction direction) { |
| 348 | + |
| 349 | + if (usesFieldRef()) { |
| 350 | + return SortArray.sortArrayOf(fieldReference).by(direction); |
| 351 | + } |
| 352 | + |
| 353 | + return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(direction); |
| 354 | + } |
| 355 | + |
339 | 356 | /**
|
340 | 357 | * Creates new {@link AggregationExpression} that transposes an array of input arrays so that the first element of
|
341 | 358 | * the output array would be an array containing, the first element of the first input array, the first element of
|
@@ -2081,10 +2098,20 @@ public SortArray byValueDescending() {
|
2081 | 2098 | return new SortArray(append("sortBy", -1));
|
2082 | 2099 | }
|
2083 | 2100 |
|
2084 |
| - /* |
2085 |
| - * (non-Javadoc) |
2086 |
| - * @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod() |
| 2101 | + /** |
| 2102 | + * Set the order to put elements in. |
| 2103 | + * |
| 2104 | + * @param direction must not be {@literal null}. |
| 2105 | + * @return new instance of {@link SortArray}. |
2087 | 2106 | */
|
| 2107 | + public SortArray by(Direction direction) { |
| 2108 | + return new SortArray(append("sortBy", direction.isAscending() ? 1 : -1)); |
| 2109 | + } |
| 2110 | + |
| 2111 | + /* |
| 2112 | + * (non-Javadoc) |
| 2113 | + * @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod() |
| 2114 | + */ |
2088 | 2115 | @Override
|
2089 | 2116 | protected String getMongoMethod() {
|
2090 | 2117 | return "$sortArray";
|
|
0 commit comments