Skip to content

Commit 35bfb92

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 89a02bb commit 35bfb92

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 com.mongodb.DBObject;
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 DBObject toDbObject(AggregationOperationContext context) {
68+
return (DBObject) 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
@@ -37,6 +37,7 @@
3737
import com.mongodb.BasicDBObject;
3838
import com.mongodb.BasicDBObjectBuilder;
3939
import com.mongodb.DBObject;
40+
import org.springframework.data.mongodb.test.util.BasicDbListBuilder;
4041

4142
/**
4243
* Unit tests for {@link Aggregation}.
@@ -595,6 +596,27 @@ public void shouldHonorDefaultCountField() {
595596
assertThat(project, isBsonObject().containing("count", 1));
596597
}
597598

599+
/**
600+
* @see DATAMONGO-1533
601+
*/
602+
@Test
603+
public void groupOperationShouldAllowUsageOfDerivedSpELAggregationExpression() {
604+
605+
DBObject agg = newAggregation( //
606+
project("a"), //
607+
group("a").first(AggregationSpELExpression.expressionOf("cond(a >= 42, 'answer', 'no-answer')")).as("foosum") //
608+
).toDbObject("foo", Aggregation.DEFAULT_CONTEXT);
609+
610+
@SuppressWarnings("unchecked")
611+
DBObject secondProjection = ((List<DBObject>) agg.get("pipeline")).get(1);
612+
DBObject fields = getAsDBObject(secondProjection, "$group");
613+
assertThat(getAsDBObject(fields, "foosum"), isBsonObject().containing("$first"));
614+
assertThat(getAsDBObject(fields, "foosum"), isBsonObject().containing("$first.$cond.if",
615+
new BasicDBObject("$gte", new BasicDbListBuilder().add("$a").add(42).get())));
616+
assertThat(getAsDBObject(fields, "foosum"), isBsonObject().containing("$first.$cond.then", "answer"));
617+
assertThat(getAsDBObject(fields, "foosum"), isBsonObject().containing("$first.$cond.else", "no-answer"));
618+
}
619+
598620
private DBObject extractPipelineElement(DBObject agg, int index, String operation) {
599621

600622
List<DBObject> pipeline = (List<DBObject>) agg.get("pipeline");

0 commit comments

Comments
 (0)