Skip to content

Add support for $rand aggregation operator. #3759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-GH-3715-SNAPSHOT</version>
<version>3.3.0-GH-3715-GH-3724-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-GH-3715-SNAPSHOT</version>
<version>3.3.0-GH-3715-GH-3724-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-GH-3715-SNAPSHOT</version>
<version>3.3.0-GH-3715-GH-3724-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-GH-3715-SNAPSHOT</version>
<version>3.3.0-GH-3715-GH-3724-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Collections;
import java.util.List;

import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.Avg;
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.Max;
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.Min;
Expand Down Expand Up @@ -54,6 +55,16 @@ public static ArithmeticOperatorFactory valueOf(AggregationExpression expression
return new ArithmeticOperatorFactory(expression);
}

/**
* Creates new {@link AggregationExpression} that returns a random float between 0 and 1 each time it is called.
*
* @return new instance of {@link Rand}.
* @since 3.3
*/
public static Rand rand() {
return new Rand();
}

/**
* @author Christoph Strobl
*/
Expand Down Expand Up @@ -1665,4 +1676,18 @@ protected String getMongoMethod() {
return "$round";
}
}

/**
* {@link Rand} returns a floating value between 0 and 1.
*
* @author Mushtaq Ahmed
* @since 3.3
*/
public static class Rand implements AggregationExpression {

@Override
public Document toDocument(AggregationOperationContext context) {
return new Document("$rand", new Document());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class MethodReferenceNode extends ExpressionNode {
map.put("subtract", arrayArgRef().forOperator("$subtract"));
map.put("trunc", singleArgRef().forOperator("$trunc"));
map.put("round", arrayArgRef().forOperator("$round"));
map.put("rand", emptyRef().forOperator("$rand"));

// STRING OPERATORS
map.put("concat", arrayArgRef().forOperator("$concat"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ void roundShouldWithPlaceFromExpression() {
.toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo(new Document("$round", Arrays.asList("$field", new Document("$first", "$source"))));
}

@Test // GH-3724
void rendersRank() {
assertThat(rand().toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo(new Document("$rand", new Document()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,11 @@ void rendersShiftWithDefault() {
.isEqualTo(Document.parse("{ $shift: { output: \"$quantity\", by: 1, default: \"Not available\" } }"));
}

@Test // GH-3724
void shouldRenderRand() {
assertThat(transform("rand()")).isEqualTo(Document.parse("{ $rand : {} }"));
}

private Object transform(String expression, Object... params) {
Object result = transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);
return result == null ? null : (!(result instanceof org.bson.Document) ? result.toString() : result);
Expand Down