Browse Source

DATAMONGO-838 - Cannot refer to expression based field in group operation.

Previously we didn't set a proper target value for the generated expression field. As a potential fix we just use the alias as the target field.

Original pull request: #116.
pull/116/merge
Thomas Darimont 12 years ago committed by Oliver Gierke
parent
commit
99eefe0773
  1. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java
  2. 20
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationUnitTests.java

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

@ -262,7 +262,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
@Override @Override
public ProjectionOperation as(String alias) { public ProjectionOperation as(String alias) {
Field expressionField = Fields.field(alias, "expr"); Field expressionField = Fields.field(alias, alias);
return this.operation.and(new ExpressionProjection(expressionField, this.value.toString(), params)); return this.operation.and(new ExpressionProjection(expressionField, this.value.toString(), params));
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,6 +28,7 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; import com.mongodb.DBObject;
/** /**
@ -161,4 +162,21 @@ public class AggregationUnitTests {
assertThat(fields.get("aCnt"), is((Object) 1)); assertThat(fields.get("aCnt"), is((Object) 1));
assertThat(fields.get("a"), is((Object) "$_id.a")); assertThat(fields.get("a"), is((Object) "$_id.a"));
} }
/**
* @see DATAMONGO-838
*/
@Test
public void expressionBasedFieldsShouldBeReferencableInFollowingOperations() {
DBObject agg = newAggregation( //
project("a").andExpression("b+c").as("foo"), //
group("a").sum("foo").as("foosum") //
).toDbObject("foo", Aggregation.DEFAULT_CONTEXT);
@SuppressWarnings("unchecked")
DBObject secondProjection = ((List<DBObject>) agg.get("pipeline")).get(1);
DBObject fields = getAsDBObject(secondProjection, "$group");
assertThat(fields.get("foosum"), is((Object) new BasicDBObject("$sum", "$foo")));
}
} }

Loading…
Cancel
Save