|
23 | 23 |
|
24 | 24 | import org.bson.Document;
|
25 | 25 | import org.springframework.data.domain.Range;
|
| 26 | +import org.springframework.data.domain.Sort; |
26 | 27 | import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.AsBuilder;
|
27 | 28 | import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.PropertyExpression;
|
28 | 29 | import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
|
@@ -315,6 +316,21 @@ public ArrayOperatorFactory.ReduceInitialValueBuilder reduce(PropertyExpression.
|
315 | 316 | .withInitialValue(initialValue).reduce(expressions);
|
316 | 317 | }
|
317 | 318 |
|
| 319 | + /** |
| 320 | + * Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given {@link Sort order}. |
| 321 | + * |
| 322 | + * @return new instance of {@link SortArray}. |
| 323 | + * @since 4.0 |
| 324 | + */ |
| 325 | + public SortArray sort(Sort sort) { |
| 326 | + |
| 327 | + if (usesFieldRef()) { |
| 328 | + return SortArray.sortArrayOf(fieldReference).by(sort); |
| 329 | + } |
| 330 | + |
| 331 | + return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(sort); |
| 332 | + } |
| 333 | + |
318 | 334 | /**
|
319 | 335 | * Creates new {@link AggregationExpression} that transposes an array of input arrays so that the first element of
|
320 | 336 | * the output array would be an array containing, the first element of the first input array, the first element of
|
@@ -1915,4 +1931,66 @@ protected String getMongoMethod() {
|
1915 | 1931 | return "$last";
|
1916 | 1932 | }
|
1917 | 1933 | }
|
| 1934 | + |
| 1935 | + /** |
| 1936 | + * {@link AggregationExpression} for {@code $sortArray} that sorts elements in an array. <br /> |
| 1937 | + * |
| 1938 | + * @author Christoph Strobl |
| 1939 | + * @since 4.0 |
| 1940 | + */ |
| 1941 | + public static class SortArray extends AbstractAggregationExpression { |
| 1942 | + |
| 1943 | + private SortArray(Object value) { |
| 1944 | + super(value); |
| 1945 | + } |
| 1946 | + |
| 1947 | + /** |
| 1948 | + * Returns the given array. |
| 1949 | + * |
| 1950 | + * @param array must not be {@literal null}. |
| 1951 | + * @return new instance of {@link SortArray}. |
| 1952 | + */ |
| 1953 | + public static SortArray sortArray(Object array) { |
| 1954 | + return new SortArray(Collections.singletonMap("input", array)); |
| 1955 | + } |
| 1956 | + |
| 1957 | + /** |
| 1958 | + * Sorts the elements in the array pointed to by the given {@link Field field reference}. |
| 1959 | + * |
| 1960 | + * @param fieldReference must not be {@literal null}. |
| 1961 | + * @return new instance of {@link SortArray}. |
| 1962 | + */ |
| 1963 | + public static SortArray sortArrayOf(String fieldReference) { |
| 1964 | + return sortArray(Fields.field(fieldReference)); |
| 1965 | + } |
| 1966 | + |
| 1967 | + /** |
| 1968 | + * Sorts the elements of the array computed buy the given {@link AggregationExpression expression}. |
| 1969 | + * |
| 1970 | + * @param expression must not be {@literal null}. |
| 1971 | + * @return new instance of {@link SortArray}. |
| 1972 | + */ |
| 1973 | + public static SortArray sortArrayOf(AggregationExpression expression) { |
| 1974 | + return sortArray(expression); |
| 1975 | + } |
| 1976 | + |
| 1977 | + /** |
| 1978 | + * Set the order to put elements in. |
| 1979 | + * |
| 1980 | + * @param sort must not be {@literal null}. |
| 1981 | + * @return new instance of {@link SortArray}. |
| 1982 | + */ |
| 1983 | + public SortArray by(Sort sort) { |
| 1984 | + return new SortArray(append("sortBy", sort)); |
| 1985 | + } |
| 1986 | + |
| 1987 | + /* |
| 1988 | + * (non-Javadoc) |
| 1989 | + * @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod() |
| 1990 | + */ |
| 1991 | + @Override |
| 1992 | + protected String getMongoMethod() { |
| 1993 | + return "$sortArray"; |
| 1994 | + } |
| 1995 | + } |
1918 | 1996 | }
|
0 commit comments