Skip to content

Commit d2f90ba

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1254 - Grouping after projection in aggregation now uses correct aliased field name.
We now push the aliased field name down the aggregation pipeline for projections including operations. This allows to reference them in a later stage. Prior to this change the field reference was potentially resolved to the target field of the operation which did not result in an error but lead to false results. Original pull request: #311.
1 parent b25fde4 commit d2f90ba

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222

2323
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
24+
import org.springframework.data.mongodb.core.aggregation.Fields.AggregationField;
2425
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation.ProjectionOperationBuilder.FieldProjection;
2526
import org.springframework.util.Assert;
2627

@@ -40,6 +41,7 @@
4041
* @author Tobias Trelle
4142
* @author Thomas Darimont
4243
* @author Oliver Gierke
44+
* @author Christoph Strobl
4345
* @since 1.3
4446
*/
4547
public class ProjectionOperation implements FieldsExposingAggregationOperation {
@@ -763,6 +765,20 @@ protected Field getField() {
763765
return field;
764766
}
765767

768+
/*
769+
* (non-Javadoc)
770+
* @see org.springframework.data.mongodb.core.aggregation.ProjectionOperation.Projection#getExposedField()
771+
*/
772+
@Override
773+
public ExposedField getExposedField() {
774+
775+
if (!getField().isAliased()) {
776+
return super.getExposedField();
777+
}
778+
779+
return new ExposedField(new AggregationField(getField().getName()), true);
780+
}
781+
766782
/**
767783
* Creates a new instance of this {@link OperationProjection} with the given alias.
768784
*

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,13 +30,15 @@
3030
import org.springframework.data.domain.Sort.Direction;
3131

3232
import com.mongodb.BasicDBObject;
33+
import com.mongodb.BasicDBObjectBuilder;
3334
import com.mongodb.DBObject;
3435

3536
/**
3637
* Unit tests for {@link Aggregation}.
3738
*
3839
* @author Oliver Gierke
3940
* @author Thomas Darimont
41+
* @author Christoph Strobl
4042
*/
4143
public class AggregationUnitTests {
4244

@@ -283,6 +285,40 @@ public void shouldSupportReferencingSystemVariables() {
283285
is((DBObject) new BasicDBObject("_id", "$someKey").append("doc", new BasicDBObject("$first", "$$ROOT"))));
284286
}
285287

288+
/**
289+
* @see DATAMONGO-1254
290+
*/
291+
@Test
292+
public void shouldExposeAliasedFieldnameForProjectionsIncludingOperationsDownThePipeline() {
293+
294+
DBObject agg = Aggregation.newAggregation(//
295+
project("date") //
296+
.and("tags").minus(10).as("tags_count")//
297+
, group("date")//
298+
.sum("tags_count").as("count")//
299+
).toDbObject("foo", Aggregation.DEFAULT_CONTEXT);
300+
301+
DBObject group = extractPipelineElement(agg, 1, "$group");
302+
assertThat(getAsDBObject(group, "count"), is(new BasicDBObjectBuilder().add("$sum", "$tags_count").get()));
303+
}
304+
305+
/**
306+
* @see DATAMONGO-1254
307+
*/
308+
@Test
309+
public void shouldUseAliasedFieldnameForProjectionsIncludingOperationsDownThePipelineWhenUsingSpEL() {
310+
311+
DBObject agg = Aggregation.newAggregation(//
312+
project("date") //
313+
.andExpression("tags-10")//
314+
, group("date")//
315+
.sum("tags_count").as("count")//
316+
).toDbObject("foo", Aggregation.DEFAULT_CONTEXT);
317+
318+
DBObject group = extractPipelineElement(agg, 1, "$group");
319+
assertThat(getAsDBObject(group, "count"), is(new BasicDBObjectBuilder().add("$sum", "$tags_count").get()));
320+
}
321+
286322
private DBObject extractPipelineElement(DBObject agg, int index, String operation) {
287323

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

0 commit comments

Comments
 (0)