Add support for sorting simple arrays (integers/strings) with SortArray ()
- Added methods `byValueAscending()` and `byValueDescending()` to the SortArray class to support sorting simple array types (e.g., integers, strings) in ascending and descending order. - Updated tests to verify the correct functionality of sorting arrays by value. - Refactored SortArray to handle sorting of simple types without requiring a property for sorting. For more details, refer to: https://www.mongodb.com/docs/manual/reference/operator/aggregation/sortArray/ Resolves: #4929 Original Pull Request: #4943 Signed-off-by: Ranzy Blessings <ranzyblessings.inbox@gmail.com>
This commit is contained in:
committed by
Christoph Strobl
parent
4bd5a1f113
commit
98f871b012
@@ -2059,6 +2059,28 @@ public class ArrayOperators {
|
||||
return new SortArray(append("sortBy", sort));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the array elements by their values in ascending order. Suitable for arrays of simple types (e.g., integers,
|
||||
* strings).
|
||||
*
|
||||
* @return new instance of {@link SortArray}.
|
||||
* @since 4.x (TBD)
|
||||
*/
|
||||
public SortArray byValueAscending() {
|
||||
return new SortArray(append("sortBy", 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the array elements by their values in descending order. Suitable for arrays of simple types (e.g., integers,
|
||||
* strings).
|
||||
*
|
||||
* @return new instance of {@link SortArray}.
|
||||
* @since 4.x (TBD)
|
||||
*/
|
||||
public SortArray byValueDescending() {
|
||||
return new SortArray(append("sortBy", -1));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.bson.Document;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.ArrayToObject;
|
||||
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.SortArray;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ArrayOperators}
|
||||
@@ -179,4 +180,27 @@ public class ArrayOperatorsUnitTests {
|
||||
assertThat(ArrayOperators.arrayOf("team").sort(Sort.by("name")).toDocument(Aggregation.DEFAULT_CONTEXT))
|
||||
.isEqualTo("{ $sortArray: { input: \"$team\", sortBy: { name: 1 } } }");
|
||||
}
|
||||
|
||||
@Test // GH-4929
|
||||
public void sortArrayByValueAscending() {
|
||||
Document result = SortArray.sortArrayOf("numbers").byValueAscending().toDocument(Aggregation.DEFAULT_CONTEXT);
|
||||
Document expected = new Document("$sortArray", new Document("input", "$numbers").append("sortBy", 1));
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test // GH-4929
|
||||
public void sortArrayByValueDescending() {
|
||||
Document result = SortArray.sortArrayOf("numbers").byValueDescending().toDocument(Aggregation.DEFAULT_CONTEXT);
|
||||
Document expected = new Document("$sortArray", new Document("input", "$numbers").append("sortBy", -1));
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test // GH-4929
|
||||
public void sortArrayByPropertyUnchanged() {
|
||||
Document result = SortArray.sortArrayOf("items").by(Sort.by(Sort.Direction.ASC, "price"))
|
||||
.toDocument(Aggregation.DEFAULT_CONTEXT);
|
||||
Document expected = new Document("$sortArray",
|
||||
new Document("input", "$items").append("sortBy", new Document("price", 1)));
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user