Skip to content

Commit fad4239

Browse files
committed
GH-4139 add support for $sortArray direction sort
for simple arrays of literals, there is no field to sort on, just have to provide direction. Signed-off-by: Nathan McDonald <[email protected]>
1 parent 4886ede commit fad4239

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ArrayOperators.java

+27
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.bson.Document;
2525
import org.springframework.data.domain.Range;
2626
import org.springframework.data.domain.Sort;
27+
import org.springframework.data.domain.Sort.Direction;
2728
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.AsBuilder;
2829
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.PropertyExpression;
2930
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
@@ -336,6 +337,22 @@ public SortArray sort(Sort sort) {
336337
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(sort);
337338
}
338339

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+
339356
/**
340357
* Creates new {@link AggregationExpression} that transposes an array of input arrays so that the first element of
341358
* the output array would be an array containing, the first element of the first input array, the first element of
@@ -2059,6 +2076,16 @@ public SortArray by(Sort sort) {
20592076
return new SortArray(append("sortBy", sort));
20602077
}
20612078

2079+
/**
2080+
* Set the order to put elements in.
2081+
*
2082+
* @param direction must not be {@literal null}.
2083+
* @return new instance of {@link SortArray}.
2084+
*/
2085+
public SortArray by(Direction direction) {
2086+
return new SortArray(append("sortBy", direction.isAscending() ? 1 : -1));
2087+
}
2088+
20622089
/*
20632090
* (non-Javadoc)
20642091
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ArrayOperatorsUnitTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.bson.Document;
2525
import org.junit.jupiter.api.Test;
2626
import org.springframework.data.domain.Sort;
27+
import org.springframework.data.domain.Sort.Direction;
2728
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.ArrayToObject;
2829

2930
/**
@@ -179,4 +180,11 @@ void sortByWithFieldRef() {
179180
assertThat(ArrayOperators.arrayOf("team").sort(Sort.by("name")).toDocument(Aggregation.DEFAULT_CONTEXT))
180181
.isEqualTo("{ $sortArray: { input: \"$team\", sortBy: { name: 1 } } }");
181182
}
183+
184+
@Test // GH-4929
185+
void sortByWithDirection() {
186+
187+
assertThat(ArrayOperators.arrayOf(List.of("a", "b", "d", "c")).sort(Direction.DESC).toDocument(Aggregation.DEFAULT_CONTEXT))
188+
.isEqualTo("{ $sortArray: { input: [\"a\", \"b\", \"d\", \"c\"], sortBy: -1 } }");
189+
}
182190
}

0 commit comments

Comments
 (0)