Browse Source

DATAMONGO-1893 - Allow exclusion of other fields than _id in aggregation $project.

As of MongoDB 3.4 exclusion of fields other than _id is allowed so we removed the limitation in our code.

Original pull request: #538.
pull/548/head
Christoph Strobl 8 years ago committed by Mark Paluch
parent
commit
f0f6185808
  1. 5
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java
  2. 9
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java

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

@ -140,11 +140,6 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { @@ -140,11 +140,6 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
*/
public ProjectionOperation andExclude(String... fieldNames) {
for (String fieldName : fieldNames) {
Assert.isTrue(Fields.UNDERSCORE_ID.equals(fieldName),
String.format(EXCLUSION_ERROR, fieldName, Fields.UNDERSCORE_ID));
}
List<FieldProjection> excludeProjections = FieldProjection.from(Fields.fields(fieldNames), false);
return new ProjectionOperation(this.projections, excludeProjections);
}

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

@ -200,10 +200,13 @@ public class ProjectionOperationUnitTests { @@ -200,10 +200,13 @@ public class ProjectionOperationUnitTests {
assertThat(oper.get(MOD)).isEqualTo((Object) Arrays.<Object> asList("$a", 3));
}
@Test(expected = IllegalArgumentException.class) // DATAMONGO-758
public void excludeShouldThrowExceptionForFieldsOtherThanUnderscoreId() {
@Test // DATAMONGO-758, DATAMONGO-1893
public void excludeShouldAllowExclusionOfFieldsOtherThanUnderscoreId/* since MongoDB 3.4 */() {
new ProjectionOperation().andExclude("foo");
ProjectionOperation projectionOp = new ProjectionOperation().andExclude("foo");
Document document = projectionOp.toDocument(Aggregation.DEFAULT_CONTEXT);
Document projectClause = DocumentTestUtils.getAsDocument(document, PROJECT);
assertThat((Integer) projectClause.get("foo")).isEqualTo(0);
}
@Test // DATAMONGO-758

Loading…
Cancel
Save