Skip to content

Commit 1b07817

Browse files
Polishing.
Update test, since tags, rename method. Original Pull Requests: #4935 & #4943
1 parent 37061db commit 1b07817

File tree

2 files changed

+25
-46
lines changed

2 files changed

+25
-46
lines changed

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

+19-30
Original file line numberDiff line numberDiff line change
@@ -338,19 +338,19 @@ public SortArray sort(Sort sort) {
338338
}
339339

340340
/**
341-
* Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given {@link Sort
342-
* order}.
341+
* Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given
342+
* {@link Direction order}.
343343
*
344344
* @return new instance of {@link SortArray}.
345-
* @since 4.0
345+
* @since 4.5
346346
*/
347347
public SortArray sort(Direction direction) {
348348

349349
if (usesFieldRef()) {
350-
return SortArray.sortArrayOf(fieldReference).by(direction);
350+
return SortArray.sortArrayOf(fieldReference).direction(direction);
351351
}
352352

353-
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(direction);
353+
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).direction(direction);
354354
}
355355

356356
/**
@@ -1960,10 +1960,6 @@ public static First firstOf(AggregationExpression expression) {
19601960
return new First(expression);
19611961
}
19621962

1963-
/*
1964-
* (non-Javadoc)
1965-
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
1966-
*/
19671963
@Override
19681964
protected String getMongoMethod() {
19691965
return "$first";
@@ -2014,10 +2010,6 @@ public static Last lastOf(AggregationExpression expression) {
20142010
return new Last(expression);
20152011
}
20162012

2017-
/*
2018-
* (non-Javadoc)
2019-
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
2020-
*/
20212013
@Override
20222014
protected String getMongoMethod() {
20232015
return "$last";
@@ -2077,41 +2069,38 @@ public SortArray by(Sort sort) {
20772069
}
20782070

20792071
/**
2080-
* Sort the array elements by their values in ascending order. Suitable for arrays of simple types (e.g., integers,
2081-
* strings).
2072+
* Order the values for the array in the given direction.
20822073
*
2074+
* @param direction must not be {@literal null}.
20832075
* @return new instance of {@link SortArray}.
2084-
* @since 4.x (TBD)
2076+
* @since 4.5
20852077
*/
2086-
public SortArray byValueAscending() {
2087-
return new SortArray(append("sortBy", 1));
2078+
public SortArray direction(Direction direction) {
2079+
return new SortArray(append("sortBy", direction.isAscending() ? 1 : -1));
20882080
}
20892081

20902082
/**
2091-
* Sort the array elements by their values in descending order. Suitable for arrays of simple types (e.g., integers,
2083+
* Sort the array elements by their values in ascending order. Suitable for arrays of simple types (e.g., integers,
20922084
* strings).
20932085
*
20942086
* @return new instance of {@link SortArray}.
2095-
* @since 4.x (TBD)
2087+
* @since 4.5
20962088
*/
2097-
public SortArray byValueDescending() {
2098-
return new SortArray(append("sortBy", -1));
2089+
public SortArray byValueAscending() {
2090+
return direction(Direction.ASC);
20992091
}
21002092

21012093
/**
2102-
* Set the order to put elements in.
2094+
* Sort the array elements by their values in descending order. Suitable for arrays of simple types (e.g., integers,
2095+
* strings).
21032096
*
2104-
* @param direction must not be {@literal null}.
21052097
* @return new instance of {@link SortArray}.
2098+
* @since 4.5
21062099
*/
2107-
public SortArray by(Direction direction) {
2108-
return new SortArray(append("sortBy", direction.isAscending() ? 1 : -1));
2100+
public SortArray byValueDescending() {
2101+
return direction(Direction.DESC);
21092102
}
21102103

2111-
/*
2112-
* (non-Javadoc)
2113-
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
2114-
*/
21152104
@Override
21162105
protected String getMongoMethod() {
21172106
return "$sortArray";

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

+6-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core.aggregation;
1717

18-
import static org.springframework.data.mongodb.test.util.Assertions.*;
18+
import static org.springframework.data.mongodb.test.util.Assertions.assertThat;
1919

2020
import java.util.ArrayList;
2121
import java.util.Arrays;
@@ -26,7 +26,6 @@
2626
import org.springframework.data.domain.Sort;
2727
import org.springframework.data.domain.Sort.Direction;
2828
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.ArrayToObject;
29-
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.SortArray;
3029

3130
/**
3231
* Unit tests for {@link ArrayOperators}
@@ -184,25 +183,16 @@ void sortByWithFieldRef() {
184183

185184
@Test // GH-4929
186185
public void sortArrayByValueAscending() {
187-
Document result = SortArray.sortArrayOf("numbers").byValueAscending().toDocument(Aggregation.DEFAULT_CONTEXT);
188-
Document expected = new Document("$sortArray", new Document("input", "$numbers").append("sortBy", 1));
189-
assertThat(result).isEqualTo(expected);
186+
187+
Document result = ArrayOperators.arrayOf("numbers").sort(Direction.ASC).toDocument(Aggregation.DEFAULT_CONTEXT);
188+
assertThat(result).isEqualTo("{ $sortArray: { input: '$numbers', sortBy: 1 } }");
190189
}
191190

192191
@Test // GH-4929
193192
public void sortArrayByValueDescending() {
194-
Document result = SortArray.sortArrayOf("numbers").byValueDescending().toDocument(Aggregation.DEFAULT_CONTEXT);
195-
Document expected = new Document("$sortArray", new Document("input", "$numbers").append("sortBy", -1));
196-
assertThat(result).isEqualTo(expected);
197-
}
198193

199-
@Test // GH-4929
200-
public void sortArrayByPropertyUnchanged() {
201-
Document result = SortArray.sortArrayOf("items").by(Sort.by(Sort.Direction.ASC, "price"))
202-
.toDocument(Aggregation.DEFAULT_CONTEXT);
203-
Document expected = new Document("$sortArray",
204-
new Document("input", "$items").append("sortBy", new Document("price", 1)));
205-
assertThat(result).isEqualTo(expected);
194+
Document result = ArrayOperators.arrayOf("numbers").sort(Direction.DESC).toDocument(Aggregation.DEFAULT_CONTEXT);
195+
assertThat(result).isEqualTo("{ $sortArray: { input: '$numbers', sortBy: -1 } }");
206196
}
207197

208198
@Test // GH-4929

0 commit comments

Comments
 (0)