Add support for $rand aggregation operator.
Closes #3724 Original pull request: #3759
This commit is contained in:
committed by
Mark Paluch
parent
24171b3ae2
commit
92cc2a582a
@@ -19,6 +19,7 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.Avg;
|
||||
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.CovariancePop;
|
||||
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.CovarianceSamp;
|
||||
@@ -63,6 +64,16 @@ public class ArithmeticOperators {
|
||||
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
|
||||
*/
|
||||
@@ -2671,4 +2682,18 @@ public class ArithmeticOperators {
|
||||
return "$tanh";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,6 +99,7 @@ public class MethodReferenceNode extends ExpressionNode {
|
||||
map.put("cosh", singleArgRef().forOperator("$cosh"));
|
||||
map.put("tan", singleArgRef().forOperator("$tan"));
|
||||
map.put("tanh", singleArgRef().forOperator("$tanh"));
|
||||
map.put("rand", emptyRef().forOperator("$rand"));
|
||||
|
||||
// STRING OPERATORS
|
||||
map.put("concat", arrayArgRef().forOperator("$concat"));
|
||||
|
||||
@@ -166,4 +166,9 @@ class ArithmeticOperatorsUnitTests {
|
||||
.isEqualTo("{ $tanh : { $degreesToRadians : \"$angle\" } }");
|
||||
}
|
||||
|
||||
|
||||
@Test // GH-3724
|
||||
void rendersRank() {
|
||||
assertThat(rand().toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo(new Document("$rand", new Document()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1044,6 +1044,11 @@ public class SpelExpressionTransformerUnitTests {
|
||||
assertThat(transform("dateDiff(purchaseDate, delivered, 'day')")).isEqualTo(Document.parse("{ $dateDiff: { startDate: \"$purchaseDate\", endDate: \"$delivered\", unit: \"day\" } }"));
|
||||
}
|
||||
|
||||
@Test // GH-3724
|
||||
void shouldRenderRand() {
|
||||
assertThat(transform("rand()")).isEqualTo(Document.parse("{ $rand : {} }"));
|
||||
}
|
||||
|
||||
private Document transform(String expression, Object... params) {
|
||||
return (Document) transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user