From f84a62b94d0590a677d91c9a9031dc9273d2df07 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 28 Sep 2023 09:05:17 +0200 Subject: [PATCH] Add expression support for median aggregation. Original Pull Request: #4515 --- .../data/mongodb/core/spel/MethodReferenceNode.java | 2 ++ .../SpelExpressionTransformerUnitTests.java | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/spel/MethodReferenceNode.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/spel/MethodReferenceNode.java index c5f51800fe..ccf2ef72d8 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/spel/MethodReferenceNode.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/spel/MethodReferenceNode.java @@ -231,6 +231,8 @@ public class MethodReferenceNode extends ExpressionNode { .mappingParametersTo("n", "input")); map.put("percentile", mapArgRef().forOperator("$percentile") // .mappingParametersTo("input", "p", "method")); + map.put("median", mapArgRef().forOperator("$median") // + .mappingParametersTo("input", "method")); // TYPE OPERATORS map.put("type", singleArgRef().forOperator("$type")); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SpelExpressionTransformerUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SpelExpressionTransformerUnitTests.java index d7aa5cfc7f..6c368b1fea 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SpelExpressionTransformerUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SpelExpressionTransformerUnitTests.java @@ -1269,6 +1269,16 @@ void shouldRenderPercentile() { .isEqualTo("{ $percentile : { input : \"$score\", p : [0.4, 0.85], method : \"approximate\" }}"); } + @Test // GH-4472 + void shouldRenderMedian() { + + assertThat(transform("median(new String[]{\"$scoreOne\", \"$scoreTwo\" }, \"approximate\")")) + .isEqualTo("{ $median : { input : [\"$scoreOne\", \"$scoreTwo\"], method : \"approximate\" }}"); + + assertThat(transform("median(score, \"approximate\")")) + .isEqualTo("{ $median : { input : \"$score\", method : \"approximate\" }}"); + } + private Document transform(String expression, Object... params) { return (Document) transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params); }