From dd446472bc0fb561dadcc3d2cc67684ab1168cec Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 22 Sep 2022 13:03:42 +0200 Subject: [PATCH] Polishing. See #4139 Original pull request: #4182. --- .../core/aggregation/SelectionOperators.java | 16 ++++++++++++---- .../aggregation/SelectionOperatorUnitTests.java | 8 ++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/SelectionOperators.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/SelectionOperators.java index d0c63093cf..c170011841 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/SelectionOperators.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/SelectionOperators.java @@ -55,6 +55,14 @@ public static Bottom bottom() { return new Bottom(Collections.emptyMap()); } + /** + * @param numberOfResults Limits the number of returned elements to the given value. + * @return new instance of {@link Bottom}. + */ + public static Bottom bottom(int numberOfResults) { + return bottom().limit(numberOfResults); + } + /** * Limits the number of returned elements to the given value. * @@ -239,14 +247,14 @@ protected First(Object value) { * @return new instance of {@link First}. */ public static First first() { - return new First(Collections.emptyMap()).limit(1); + return new First(Collections.emptyMap()); } /** * @return new instance of {@link First}. */ public static First first(int numberOfResults) { - return new First(Collections.emptyMap()).limit(numberOfResults); + return first().limit(numberOfResults); } /** @@ -333,14 +341,14 @@ protected Last(Object value) { * @return new instance of {@link Last}. */ public static Last last() { - return new Last(Collections.emptyMap()).limit(1); + return new Last(Collections.emptyMap()); } /** * @return new instance of {@link Last}. */ public static Last last(int numberOfResults) { - return new Last(Collections.emptyMap()).limit(numberOfResults); + return last().limit(numberOfResults); } /** diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SelectionOperatorUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SelectionOperatorUnitTests.java index 287cebc455..508963cc74 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SelectionOperatorUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SelectionOperatorUnitTests.java @@ -74,8 +74,8 @@ void bottomMapsFieldNamesCorrectly() { @Test // GH-4139 void bottomNRenderedCorrectly() { - Document document = SelectionOperators.Bottom.bottom().output(Fields.fields("playerId", "score")) - .sortBy(Sort.by(Direction.DESC, "score")).limit(3).toDocument(Aggregation.DEFAULT_CONTEXT); + Document document = SelectionOperators.Bottom.bottom(3).output(Fields.fields("playerId", "score")) + .sortBy(Sort.by(Direction.DESC, "score")).toDocument(Aggregation.DEFAULT_CONTEXT); assertThat(document).isEqualTo(Document.parse(""" { @@ -114,8 +114,8 @@ void topMapsFieldNamesCorrectly() { @Test // GH-4139 void topNRenderedCorrectly() { - Document document = SelectionOperators.Top.top().output(Fields.fields("playerId", "score")) - .sortBy(Sort.by(Direction.DESC, "score")).limit(3).toDocument(Aggregation.DEFAULT_CONTEXT); + Document document = SelectionOperators.Top.top(3).output(Fields.fields("playerId", "score")) + .sortBy(Sort.by(Direction.DESC, "score")).toDocument(Aggregation.DEFAULT_CONTEXT); assertThat(document).isEqualTo(Document.parse(""" {