Skip to content

Commit 7fde466

Browse files
author
Mushtaq Ahmed
committed
Add support for $rand aggregation operator.
Original pull request spring-projects#3741 Closes spring-projects#3724
1 parent 3ad5429 commit 7fde466

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

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

+25
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.data.mongodb.core.aggregation.AccumulatorOperators.Avg;
2223
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.Max;
2324
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.Min;
@@ -54,6 +55,16 @@ public static ArithmeticOperatorFactory valueOf(AggregationExpression expression
5455
return new ArithmeticOperatorFactory(expression);
5556
}
5657

58+
/**
59+
* Creates new {@link AggregationExpression} that returns a random float between 0 and 1 each time it is called.
60+
*
61+
* @return new instance of {@link Rand}.
62+
* @since 3.3
63+
*/
64+
public static Rand rand() {
65+
return new Rand();
66+
}
67+
5768
/**
5869
* @author Christoph Strobl
5970
*/
@@ -1665,4 +1676,18 @@ protected String getMongoMethod() {
16651676
return "$round";
16661677
}
16671678
}
1679+
1680+
/**
1681+
* {@link Rand} returns a floating value between 0 and 1.
1682+
*
1683+
* @author Mushtaq Ahmed
1684+
* @since 3.3
1685+
*/
1686+
public static class Rand implements AggregationExpression {
1687+
1688+
@Override
1689+
public Document toDocument(AggregationOperationContext context) {
1690+
return new Document("$rand", new Document());
1691+
}
1692+
}
16681693
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public class MethodReferenceNode extends ExpressionNode {
9191
map.put("subtract", arrayArgRef().forOperator("$subtract"));
9292
map.put("trunc", singleArgRef().forOperator("$trunc"));
9393
map.put("round", arrayArgRef().forOperator("$round"));
94+
map.put("rand", emptyRef().forOperator("$rand"));
9495

9596
// STRING OPERATORS
9697
map.put("concat", arrayArgRef().forOperator("$concat"));

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

+5
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,9 @@ void roundShouldWithPlaceFromExpression() {
5959
.toDocument(Aggregation.DEFAULT_CONTEXT))
6060
.isEqualTo(new Document("$round", Arrays.asList("$field", new Document("$first", "$source"))));
6161
}
62+
63+
@Test // GH-3724
64+
void rendersRank() {
65+
assertThat(rand().toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo(new Document("$rand", new Document()));
66+
}
6267
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,11 @@ void rendersShiftWithDefault() {
975975
.isEqualTo(Document.parse("{ $shift: { output: \"$quantity\", by: 1, default: \"Not available\" } }"));
976976
}
977977

978+
@Test // GH-3724
979+
void shouldRenderRand() {
980+
assertThat(transform("rand()")).isEqualTo(Document.parse("{ $rand : {} }"));
981+
}
982+
978983
private Object transform(String expression, Object... params) {
979984
Object result = transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);
980985
return result == null ? null : (!(result instanceof org.bson.Document) ? result.toString() : result);

0 commit comments

Comments
 (0)