Skip to content

Commit b731789

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1533 - Add AggregationExpression derived from SpEL AST.
We added an AggregationExpression that renders a MongoDB Aggregation Framework expression from the AST of a SpEL expression. This allows usage with various stages (eg. $project, $group) throughout the aggregation support. // { $and: [ { $gt: [ "$qty", 100 ] }, { $lt: [ "$qty", 250 ] } ] } expressionOf("qty > 100 && qty < 250); // { $cond : { if : { $gte : [ "$a", 42 ]}, then : "answer", else : "no-answer" } } expressionOf("cond(a >= 42, 'answer', 'no-answer')"); Original pull request: #428.
1 parent 35f43e9 commit b731789

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2016. the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.aggregation;
17+
18+
import org.bson.Document;
19+
import org.springframework.util.Assert;
20+
21+
/**
22+
* An {@link AggregationExpression} that renders a MongoDB Aggregation Framework expression from the AST of a
23+
* <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html">SpEL
24+
* expression</a>. <br />
25+
* <br />
26+
* <strong>Samples:</strong> <br />
27+
* <code>
28+
* <pre>
29+
* // { $and: [ { $gt: [ "$qty", 100 ] }, { $lt: [ "$qty", 250 ] } ] }
30+
* expressionOf("qty > 100 && qty < 250);
31+
*
32+
* // { $cond : { if : { $gte : [ "$a", 42 ]}, then : "answer", else : "no-answer" } }
33+
* expressionOf("cond(a >= 42, 'answer', 'no-answer')");
34+
* </pre>
35+
* </code>
36+
*
37+
* @author Christoph Strobl
38+
* @see SpelExpressionTransformer
39+
* @since 1.10
40+
*/
41+
public class AggregationSpELExpression implements AggregationExpression {
42+
43+
private static final SpelExpressionTransformer TRANSFORMER = new SpelExpressionTransformer();
44+
private final String rawExpression;
45+
private final Object[] parameters;
46+
47+
private AggregationSpELExpression(String rawExpression, Object[] parameters) {
48+
49+
this.rawExpression = rawExpression;
50+
this.parameters = parameters;
51+
}
52+
53+
/**
54+
* Creates new {@link AggregationSpELExpression} for the given {@literal expressionString} and {@literal parameters}.
55+
*
56+
* @param expression must not be {@literal null}.
57+
* @param parameters can be empty.
58+
* @return
59+
*/
60+
public static AggregationSpELExpression expressionOf(String expressionString, Object... parameters) {
61+
62+
Assert.notNull(expressionString, "ExpressionString must not be null!");
63+
return new AggregationSpELExpression(expressionString, parameters);
64+
}
65+
66+
@Override
67+
public Document toDocument(AggregationOperationContext context) {
68+
return (Document) TRANSFORMER.transform(rawExpression, context, parameters);
69+
}
70+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ public void shouldUseAliasedFieldnameForProjectionsIncludingOperationsDownThePip
393393
Document group = extractPipelineElement(agg, 1, "$group");
394394
assertThat(getAsDocument(group, "count"), is(new Document().append("$sum", "$tags_count")));
395395
}
396+
396397
/**
397398
* @see DATAMONGO-861
398399
*/
@@ -590,6 +591,27 @@ public void shouldHonorDefaultCountField() {
590591
assertThat(project, isBsonObject().containing("count", 1));
591592
}
592593

594+
/**
595+
* @see DATAMONGO-1533
596+
*/
597+
@Test
598+
public void groupOperationShouldAllowUsageOfDerivedSpELAggregationExpression() {
599+
600+
Document agg = newAggregation( //
601+
project("a"), //
602+
group("a").first(AggregationSpELExpression.expressionOf("cond(a >= 42, 'answer', 'no-answer')")).as("foosum") //
603+
).toDocument("foo", Aggregation.DEFAULT_CONTEXT);
604+
605+
@SuppressWarnings("unchecked")
606+
Document secondProjection = ((List<Document>) agg.get("pipeline")).get(1);
607+
Document fields = getAsDocument(secondProjection, "$group");
608+
assertThat(getAsDocument(fields, "foosum"), isBsonObject().containing("$first"));
609+
assertThat(getAsDocument(fields, "foosum"),
610+
isBsonObject().containing("$first.$cond.if", new Document("$gte", new Document("$a", 42))));
611+
assertThat(getAsDocument(fields, "foosum"), isBsonObject().containing("$first.$cond.then", "answer"));
612+
assertThat(getAsDocument(fields, "foosum"), isBsonObject().containing("$first.$cond.else", "no-answer"));
613+
}
614+
593615
private Document extractPipelineElement(Document agg, int index, String operation) {
594616

595617
List<Document> pipeline = (List<Document>) agg.get("pipeline");

0 commit comments

Comments
 (0)