Skip to content

Commit 92842fc

Browse files
Add support for $rank & $denseRank aggregation operators.
Closes: #3715
1 parent cd526d0 commit 92842fc

File tree

5 files changed

+118
-2
lines changed

5 files changed

+118
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ComparisonOperators.java

+52
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Collections;
1919
import java.util.List;
2020

21+
import org.bson.Document;
2122
import org.springframework.util.Assert;
2223

2324
/**
@@ -48,6 +49,26 @@ public static ComparisonOperatorFactory valueOf(AggregationExpression expression
4849
return new ComparisonOperatorFactory(expression);
4950
}
5051

52+
/**
53+
* Obtain the document position (including gaps) relative to others (rank).
54+
*
55+
* @return new instance of {@link Rank}.
56+
* @since 3.3
57+
*/
58+
public static Rank rank() {
59+
return new Rank();
60+
}
61+
62+
/**
63+
* Obtain the document position (without gaps) relative to others (rank).
64+
*
65+
* @return new instance of {@link DenseRank}.
66+
* @since 3.3
67+
*/
68+
public static DenseRank denseRank() {
69+
return new DenseRank();
70+
}
71+
5172
public static class ComparisonOperatorFactory {
5273

5374
private final String fieldReference;
@@ -876,4 +897,35 @@ public Ne notEqualToValue(Object value) {
876897
return new Ne(append(value, Expand.KEEP_SOURCE));
877898
}
878899
}
900+
901+
/**
902+
* {@link Rank} resolves the current document position (the rank) relative to other documents. If multiple documents
903+
* occupy the same rank, {@literal $rank} places the document with the subsequent value at a rank with a gap.
904+
*
905+
* @author Christoph Strobl
906+
* @since 3.3
907+
*/
908+
public static class Rank implements AggregationExpression {
909+
910+
@Override
911+
public Document toDocument(AggregationOperationContext context) {
912+
return new Document("$rank", new Document());
913+
}
914+
}
915+
916+
/**
917+
* {@link DenseRank} resolves the current document position (the rank) relative to other documents. If multiple
918+
* documents occupy the same rank, {@literal $denseRank} places the document with the subsequent value at the next rank without
919+
* any gaps.
920+
*
921+
* @author Christoph Strobl
922+
* @since 3.3
923+
*/
924+
public static class DenseRank implements AggregationExpression {
925+
926+
@Override
927+
public Document toDocument(AggregationOperationContext context) {
928+
return new Document("$denseRank", new Document());
929+
}
930+
}
879931
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/SpelExpressionTransformer.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,10 @@ protected Object convert(AggregationExpressionTransformationContext<MethodRefere
500500
dbo.put(methodReference.getArgumentMap()[i++], transform(child, context));
501501
}
502502
args = dbo;
503-
} else {
503+
} else if (ObjectUtils.nullSafeEquals(methodReference.getArgumentType(), ArgumentType.EMPTY_DOCUMENT)) {
504+
args = new Document();
505+
}
506+
else {
504507

505508
List<Object> argList = new ArrayList<Object>();
506509

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/spel/MethodReferenceNode.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public class MethodReferenceNode extends ExpressionNode {
6767
map.put("lt", arrayArgRef().forOperator("$lt"));
6868
map.put("lte", arrayArgRef().forOperator("$lte"));
6969
map.put("ne", arrayArgRef().forOperator("$ne"));
70+
map.put("rank", emptyRef().forOperator("$rank"));
71+
map.put("denseRank", emptyRef().forOperator("$denseRank"));
7072

7173
// ARITHMETIC OPERATORS
7274
map.put("abs", singleArgRef().forOperator("$abs"));
@@ -305,6 +307,16 @@ static AggregationMethodReference mapArgRef() {
305307
return new AggregationMethodReference(null, ArgumentType.MAP, null);
306308
}
307309

310+
/**
311+
* Create a new {@link AggregationMethodReference} for a {@link ArgumentType#EMPTY_DOCUMENT} argument.
312+
*
313+
* @return never {@literal null}.
314+
* @since 3.3
315+
*/
316+
static AggregationMethodReference emptyRef() {
317+
return new AggregationMethodReference(null, ArgumentType.EMPTY_DOCUMENT, null);
318+
}
319+
308320
/**
309321
* Create a new {@link AggregationMethodReference} for a given {@literal aggregationExpressionOperator} reusing
310322
* previously set arguments.
@@ -340,7 +352,7 @@ AggregationMethodReference mappingParametersTo(String... aggregationExpressionPr
340352
* @since 1.10
341353
*/
342354
public enum ArgumentType {
343-
SINGLE, ARRAY, MAP
355+
SINGLE, ARRAY, MAP, EMPTY_DOCUMENT
344356
}
345357
}
346358

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.aggregation;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.data.mongodb.core.aggregation.ComparisonOperators.*;
20+
21+
import org.bson.Document;
22+
import org.junit.jupiter.api.Test;
23+
24+
/**
25+
* @author Christoph Strobl
26+
*/
27+
class ComparisonOperatorsUnitTests {
28+
29+
@Test // GH-3715
30+
void rendersRank() {
31+
assertThat(rank().toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo(new Document("$rank", new Document()));
32+
}
33+
34+
@Test // GH-3715
35+
void rendersDenseRank() {
36+
assertThat(denseRank().toDocument(Aggregation.DEFAULT_CONTEXT))
37+
.isEqualTo(new Document("$denseRank", new Document()));
38+
}
39+
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SpelExpressionTransformerUnitTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,16 @@ public void shouldRenderRoundWithPlace() {
946946
assertThat(transform("round(field, 2)")).isEqualTo(Document.parse("{ \"$round\" : [\"$field\", 2]}"));
947947
}
948948

949+
@Test // GH-3715
950+
void shouldRenderRank() {
951+
assertThat(transform("rank()")).isEqualTo(Document.parse("{ $rank : {} }"));
952+
}
953+
954+
@Test // GH-3715
955+
void shouldRenderDenseRank() {
956+
assertThat(transform("denseRank()")).isEqualTo(Document.parse("{ $denseRank : {} }"));
957+
}
958+
949959
private Object transform(String expression, Object... params) {
950960
Object result = transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);
951961
return result == null ? null : (!(result instanceof org.bson.Document) ? result.toString() : result);

0 commit comments

Comments
 (0)