Add support for sorting simple arrays by direction.
Add method to provide sorting direction to sort array aggregation. Related to: #4929 Original Pull Request: #4935 Signed-off-by: Nathan McDonald <nathan.mcdonald@uk.ey.com>
This commit is contained in:
committed by
Christoph Strobl
parent
98f871b012
commit
5db56b2d4d
@@ -24,6 +24,7 @@ import java.util.List;
|
||||
import org.bson.Document;
|
||||
import org.springframework.data.domain.Range;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.AsBuilder;
|
||||
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.PropertyExpression;
|
||||
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
|
||||
@@ -336,6 +337,22 @@ public class ArrayOperators {
|
||||
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(sort);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given {@link Sort
|
||||
* order}.
|
||||
*
|
||||
* @return new instance of {@link SortArray}.
|
||||
* @since 4.0
|
||||
*/
|
||||
public SortArray sort(Direction direction) {
|
||||
|
||||
if (usesFieldRef()) {
|
||||
return SortArray.sortArrayOf(fieldReference).by(direction);
|
||||
}
|
||||
|
||||
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new {@link AggregationExpression} that transposes an array of input arrays so that the first element of
|
||||
* 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 class ArrayOperators {
|
||||
return new SortArray(append("sortBy", -1));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
|
||||
/**
|
||||
* Set the order to put elements in.
|
||||
*
|
||||
* @param direction must not be {@literal null}.
|
||||
* @return new instance of {@link SortArray}.
|
||||
*/
|
||||
public SortArray by(Direction direction) {
|
||||
return new SortArray(append("sortBy", direction.isAscending() ? 1 : -1));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
|
||||
*/
|
||||
@Override
|
||||
protected String getMongoMethod() {
|
||||
return "$sortArray";
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.List;
|
||||
import org.bson.Document;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.ArrayToObject;
|
||||
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.SortArray;
|
||||
|
||||
@@ -203,4 +204,12 @@ public class ArrayOperatorsUnitTests {
|
||||
new Document("input", "$items").append("sortBy", new Document("price", 1)));
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test // GH-4929
|
||||
void sortByWithDirection() {
|
||||
|
||||
assertThat(ArrayOperators.arrayOf(List.of("a", "b", "d", "c")).sort(Direction.DESC)
|
||||
.toDocument(Aggregation.DEFAULT_CONTEXT))
|
||||
.isEqualTo("{ $sortArray: { input: [\"a\", \"b\", \"d\", \"c\"], sortBy: -1 } }");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user