Skip to content

Commit 09e20b9

Browse files
author
Thomas Darimont
committed
DATAMONGO-1133 - Field aliasing is not honored in Aggregation operations.
Added some test to show that we now use the field name for resolving the property path. Previously we used the field target name as is which didn’t honour field aliases.
1 parent 926d1ed commit 09e20b9

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ private void cleanDb() {
125125
mongoTemplate.dropCollection(Person.class);
126126
mongoTemplate.dropCollection(Reservation.class);
127127
mongoTemplate.dropCollection(Venue.class);
128+
mongoTemplate.dropCollection(MeterData.class);
128129
}
129130

130131
/**
@@ -1044,6 +1045,30 @@ public void shouldSupportGeoNearQueriesForAggregationWithDistanceField() {
10441045
assertThat((Double) firstResult.get("distance"), closeTo(117.620092203928, 0.00001));
10451046
}
10461047

1048+
/**
1049+
* @see DATAMONGO-1133
1050+
*/
1051+
@Test
1052+
public void shouldHonorFieldAliasesForFieldReferences() {
1053+
1054+
mongoTemplate.insert(new MeterData("m1", "counter1", 42));
1055+
mongoTemplate.insert(new MeterData("m1", "counter1", 13));
1056+
mongoTemplate.insert(new MeterData("m1", "counter1", 45));
1057+
1058+
TypedAggregation<MeterData> agg = newAggregation(MeterData.class, //
1059+
match(where("resourceId").is("m1")), //
1060+
group("counterName").sum("counterVolume").as("totalValue") //
1061+
);
1062+
1063+
AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, DBObject.class);
1064+
1065+
assertThat(results.getMappedResults(), hasSize(1));
1066+
DBObject result = results.getMappedResults().get(0);
1067+
1068+
assertThat(result.get("_id"), is(equalTo((Object) "counter1")));
1069+
assertThat(result.get("totalValue"), is(equalTo((Object) 42.0)));
1070+
}
1071+
10471072
private void assertLikeStats(LikeStats like, String id, long count) {
10481073

10491074
assertThat(like, is(notNullValue()));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2015 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.springframework.data.annotation.Id;
19+
import org.springframework.data.mongodb.core.mapping.Field;
20+
21+
/**
22+
* @author Thomas Darimont
23+
*/
24+
public class MeterData {
25+
26+
@Id String resourceId;
27+
@Field("counter_name") String counterName;
28+
double counterVolume;
29+
30+
public MeterData() {}
31+
32+
public MeterData(String resourceId, String counterName, double counterVolume) {
33+
34+
this.resourceId = resourceId;
35+
this.counterName = counterName;
36+
this.counterVolume = counterVolume;
37+
}
38+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,24 @@ public void rendersAggregationOptionsInTypedAggregationContextCorrectly() {
169169
assertThat(dbo.get("cursor"), is((Object) new BasicDBObject("foo", 1)));
170170
}
171171

172+
/**
173+
* @see DATAMONGO-1133
174+
*/
175+
@Test
176+
public void shouldHonorAliasedFieldsInGroupExpressions() {
177+
178+
TypeBasedAggregationOperationContext context = getContext(MeterData.class);
179+
TypedAggregation<MeterData> agg = newAggregation(MeterData.class,
180+
group("counterName").sum("counterVolume").as("totalCounterVolume"));
181+
182+
DBObject dbo = agg.toDbObject("meterData", context);
183+
DBObject group = getPipelineElementFromAggregationAt(dbo, 0);
184+
185+
DBObject definition = (DBObject) group.get("$group");
186+
187+
assertThat(definition.get("_id"), is(equalTo((Object) "$counter_name")));
188+
}
189+
172190
@Document(collection = "person")
173191
public static class FooPerson {
174192

0 commit comments

Comments
 (0)