Skip to content

Commit f84a62b

Browse files
Add expression support for median aggregation.
Original Pull Request: #4515
1 parent d506cd0 commit f84a62b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ public class MethodReferenceNode extends ExpressionNode {
231231
.mappingParametersTo("n", "input"));
232232
map.put("percentile", mapArgRef().forOperator("$percentile") //
233233
.mappingParametersTo("input", "p", "method"));
234+
map.put("median", mapArgRef().forOperator("$median") //
235+
.mappingParametersTo("input", "method"));
234236

235237
// TYPE OPERATORS
236238
map.put("type", singleArgRef().forOperator("$type"));

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

+10
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,16 @@ void shouldRenderPercentile() {
12691269
.isEqualTo("{ $percentile : { input : \"$score\", p : [0.4, 0.85], method : \"approximate\" }}");
12701270
}
12711271

1272+
@Test // GH-4472
1273+
void shouldRenderMedian() {
1274+
1275+
assertThat(transform("median(new String[]{\"$scoreOne\", \"$scoreTwo\" }, \"approximate\")"))
1276+
.isEqualTo("{ $median : { input : [\"$scoreOne\", \"$scoreTwo\"], method : \"approximate\" }}");
1277+
1278+
assertThat(transform("median(score, \"approximate\")"))
1279+
.isEqualTo("{ $median : { input : \"$score\", method : \"approximate\" }}");
1280+
}
1281+
12721282
private Document transform(String expression, Object... params) {
12731283
return (Document) transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);
12741284
}

0 commit comments

Comments
 (0)