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.
This commit is contained in:
Christoph Strobl
2018-03-20 10:35:33 +01:00
committed by Mark Paluch
parent aea40ca490
commit 7f9ab3bb44
2 changed files with 6 additions and 8 deletions

View File

@@ -140,11 +140,6 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
*/ */
public ProjectionOperation andExclude(String... fieldNames) { 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); List<FieldProjection> excludeProjections = FieldProjection.from(Fields.fields(fieldNames), false);
return new ProjectionOperation(this.projections, excludeProjections); return new ProjectionOperation(this.projections, excludeProjections);
} }

View File

@@ -198,10 +198,13 @@ public class ProjectionOperationUnitTests {
assertThat(oper.get(MOD)).isEqualTo((Object) Arrays.<Object> asList("$a", 3)); assertThat(oper.get(MOD)).isEqualTo((Object) Arrays.<Object> asList("$a", 3));
} }
@Test(expected = IllegalArgumentException.class) // DATAMONGO-758 @Test // DATAMONGO-758, DATAMONGO-1893
public void excludeShouldThrowExceptionForFieldsOtherThanUnderscoreId() { 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 @Test // DATAMONGO-758