Polishing.
Update test, since tags, rename method. Original Pull Requests: #4935 & #4943
This commit is contained in:
@@ -338,19 +338,19 @@ public class ArrayOperators {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given {@link Sort
|
||||
* order}.
|
||||
* Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given
|
||||
* {@link Direction order}.
|
||||
*
|
||||
* @return new instance of {@link SortArray}.
|
||||
* @since 4.0
|
||||
* @since 4.5
|
||||
*/
|
||||
public SortArray sort(Direction direction) {
|
||||
|
||||
if (usesFieldRef()) {
|
||||
return SortArray.sortArrayOf(fieldReference).by(direction);
|
||||
return SortArray.sortArrayOf(fieldReference).direction(direction);
|
||||
}
|
||||
|
||||
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(direction);
|
||||
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).direction(direction);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1960,10 +1960,6 @@ public class ArrayOperators {
|
||||
return new First(expression);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
|
||||
*/
|
||||
@Override
|
||||
protected String getMongoMethod() {
|
||||
return "$first";
|
||||
@@ -2014,10 +2010,6 @@ public class ArrayOperators {
|
||||
return new Last(expression);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
|
||||
*/
|
||||
@Override
|
||||
protected String getMongoMethod() {
|
||||
return "$last";
|
||||
@@ -2076,15 +2068,26 @@ public class ArrayOperators {
|
||||
return new SortArray(append("sortBy", sort));
|
||||
}
|
||||
|
||||
/**
|
||||
* Order the values for the array in the given direction.
|
||||
*
|
||||
* @param direction must not be {@literal null}.
|
||||
* @return new instance of {@link SortArray}.
|
||||
* @since 4.5
|
||||
*/
|
||||
public SortArray direction(Direction direction) {
|
||||
return new SortArray(append("sortBy", direction.isAscending() ? 1 : -1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
* @since 4.5
|
||||
*/
|
||||
public SortArray byValueAscending() {
|
||||
return new SortArray(append("sortBy", 1));
|
||||
return direction(Direction.ASC);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2092,26 +2095,12 @@ public class ArrayOperators {
|
||||
* strings).
|
||||
*
|
||||
* @return new instance of {@link SortArray}.
|
||||
* @since 4.x (TBD)
|
||||
* @since 4.5
|
||||
*/
|
||||
public SortArray byValueDescending() {
|
||||
return new SortArray(append("sortBy", -1));
|
||||
return direction(Direction.DESC);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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";
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.aggregation;
|
||||
|
||||
import static org.springframework.data.mongodb.test.util.Assertions.*;
|
||||
import static org.springframework.data.mongodb.test.util.Assertions.assertThat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -26,7 +26,6 @@ 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;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ArrayOperators}
|
||||
@@ -184,25 +183,16 @@ public class ArrayOperatorsUnitTests {
|
||||
|
||||
@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);
|
||||
|
||||
Document result = ArrayOperators.arrayOf("numbers").sort(Direction.ASC).toDocument(Aggregation.DEFAULT_CONTEXT);
|
||||
assertThat(result).isEqualTo("{ $sortArray: { input: '$numbers', sortBy: 1 } }");
|
||||
}
|
||||
|
||||
@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);
|
||||
Document result = ArrayOperators.arrayOf("numbers").sort(Direction.DESC).toDocument(Aggregation.DEFAULT_CONTEXT);
|
||||
assertThat(result).isEqualTo("{ $sortArray: { input: '$numbers', sortBy: -1 } }");
|
||||
}
|
||||
|
||||
@Test // GH-4929
|
||||
|
||||
Reference in New Issue
Block a user