Browse Source
Also, allow construction of $match with an AggregationExpression. Closes #3790pull/3802/head
8 changed files with 187 additions and 3 deletions
@ -0,0 +1,109 @@
@@ -0,0 +1,109 @@
|
||||
package org.springframework.data.mongodb.core.aggregation; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
public class EvaluationOperators { |
||||
|
||||
/** |
||||
* Take the value resulting from the given fieldReference. |
||||
* |
||||
* @param fieldReference must not be {@literal null}. |
||||
* @return new instance of {@link EvaluationOperatorFactory}. |
||||
*/ |
||||
public static EvaluationOperatorFactory valueOf(String fieldReference) { |
||||
return new EvaluationOperatorFactory(fieldReference); |
||||
} |
||||
|
||||
/** |
||||
* Take the value resulting from the given {@link AggregationExpression}. |
||||
* |
||||
* @param expression must not be {@literal null}. |
||||
* @return new instance of {@link EvaluationOperatorFactory}. |
||||
*/ |
||||
public static EvaluationOperatorFactory valueOf(AggregationExpression expression) { |
||||
return new EvaluationOperatorFactory(expression); |
||||
} |
||||
|
||||
public static class EvaluationOperatorFactory { |
||||
|
||||
private final String fieldReference; |
||||
private final AggregationExpression expression; |
||||
|
||||
/** |
||||
* Creates new {@link EvaluationOperatorFactory} for given {@literal fieldReference}. |
||||
* |
||||
* @param fieldReference must not be {@literal null}. |
||||
*/ |
||||
public EvaluationOperatorFactory(String fieldReference) { |
||||
|
||||
Assert.notNull(fieldReference, "FieldReference must not be null!"); |
||||
this.fieldReference = fieldReference; |
||||
this.expression = null; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Creates new {@link EvaluationOperatorFactory} for given {@link AggregationExpression}. |
||||
* |
||||
* @param expression must not be {@literal null}. |
||||
*/ |
||||
public EvaluationOperatorFactory(AggregationExpression expression) { |
||||
|
||||
Assert.notNull(expression, "Expression must not be null!"); |
||||
this.fieldReference = null; |
||||
this.expression = expression; |
||||
} |
||||
|
||||
/** |
||||
* Creates new {@link AggregationExpression} that is a valid aggregation expression. |
||||
* |
||||
* @return new instance of {@link Expr}. |
||||
*/ |
||||
public Expr expr() { |
||||
return usesFieldRef() ? Expr.valueOf(fieldReference) : Expr.valueOf(expression); |
||||
} |
||||
|
||||
|
||||
public static class Expr extends AbstractAggregationExpression { |
||||
|
||||
private Expr(Object value) { |
||||
super(value); |
||||
} |
||||
|
||||
@Override |
||||
protected String getMongoMethod() { |
||||
return "$expr"; |
||||
} |
||||
|
||||
/** |
||||
* Creates new {@link Expr}. |
||||
* |
||||
* @param fieldReference must not be {@literal null}. |
||||
* @return new instance of {@link Expr}. |
||||
*/ |
||||
public static Expr valueOf(String fieldReference) { |
||||
|
||||
Assert.notNull(fieldReference, "FieldReference must not be null!"); |
||||
return new Expr(Fields.field(fieldReference)); |
||||
} |
||||
|
||||
/** |
||||
* Creates new {@link Expr}. |
||||
* |
||||
* @param expression must not be {@literal null}. |
||||
* @return new instance of {@link Expr}. |
||||
*/ |
||||
public static Expr valueOf(AggregationExpression expression) { |
||||
|
||||
Assert.notNull(expression, "Expression must not be null!"); |
||||
return new Expr(expression); |
||||
} |
||||
|
||||
} |
||||
|
||||
private boolean usesFieldRef() { |
||||
return fieldReference != null; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
package org.springframework.data.mongodb.core.aggregation; |
||||
|
||||
import static org.assertj.core.api.Assertions.*; |
||||
|
||||
import org.bson.Document; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
class MatchOperationUnitTests { |
||||
|
||||
@Test // DATAMONGO - 3729
|
||||
public void shouldRenderStdDevPopCorrectly() { |
||||
MatchOperation operation = Aggregation.match().withValueOf(ArithmeticOperators.valueOf("quiz").stdDevPop()); |
||||
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT)). |
||||
isEqualTo(Document.parse("{ $match: { \"$expr\" : { \"$stdDevPop\" : \"$quiz\" } } } ")); |
||||
|
||||
} |
||||
|
||||
@Test // DATAMONGO - 3729
|
||||
public void shouldRenderStdDevSampCorrectly() { |
||||
MatchOperation operation = Aggregation.match().withValueOf(ArithmeticOperators.valueOf("quiz").stdDevSamp()); |
||||
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT)). |
||||
isEqualTo(Document.parse("{ $match: { \"$expr\" : { \"$stdDevSamp\" : \"$quiz\" } } } ")); |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue