Skip to content

Commit f7540d4

Browse files
Thomas Darimontodrotbohm
Thomas Darimont
authored andcommitted
DATAMONGO-758 - Current mongodb Versions only support to explicitly exclude the _id property in a projection.
Added guard to FieldProjection.from within ProjectionOption to prevent users from excluding fields other than "_id".
1 parent 3d2ae81 commit f7540d4

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ public DBObject toDBObject(AggregationOperationContext context) {
362362
* A {@link FieldProjection} to map a result of a previous {@link AggregationOperation} to a new field.
363363
*
364364
* @author Oliver Gierke
365+
* @author Thomas Darimont
365366
*/
366367
static class FieldProjection extends Projection {
367368

@@ -399,6 +400,17 @@ public static List<FieldProjection> from(Fields fields, boolean include) {
399400
List<FieldProjection> projections = new ArrayList<FieldProjection>();
400401

401402
for (Field field : fields) {
403+
404+
if (!include) {
405+
if (!Fields.UNDERSCORE_ID.equals(field.getName())) {
406+
throw new IllegalArgumentException(
407+
String
408+
.format(
409+
"Exclusion of field %s not allowed. Projections by the mongodb aggregation framework only support the exclusion of the %s field!",
410+
field.getName(), Fields.UNDERSCORE_ID));
411+
}
412+
}
413+
402414
projections.add(new FieldProjection(field, include ? null : 0));
403415
}
404416

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Unit tests for {@link ProjectionOperation}.
3232
*
3333
* @author Oliver Gierke
34+
* @author Thomas Darimont
3435
*/
3536
public class ProjectionOperationUnitTests {
3637

@@ -183,6 +184,27 @@ public void arithmenticProjectionOperationMod() {
183184
assertThat(oper.get(MOD), is((Object) Arrays.<Object> asList("$a", 3)));
184185
}
185186

187+
/**
188+
* @see DATAMONGO-758
189+
*/
190+
@Test(expected = IllegalArgumentException.class)
191+
public void excludeShouldThrowExceptionForFieldsOtherThanUnderscoreId() {
192+
193+
new ProjectionOperation().andExclude("foo");
194+
}
195+
196+
/**
197+
* @see DATAMONGO-758
198+
*/
199+
@Test
200+
public void excludeShouldExclusionOfUnderscoreId() {
201+
202+
ProjectionOperation projectionOp = new ProjectionOperation().andExclude(Fields.UNDERSCORE_ID);
203+
DBObject dbObject = projectionOp.toDBObject(Aggregation.DEFAULT_CONTEXT);
204+
DBObject projectClause = DBObjectUtils.getAsDBObject(dbObject, PROJECT);
205+
assertThat((Integer) projectClause.get(Fields.UNDERSCORE_ID), is(0));
206+
}
207+
186208
@Test(expected = IllegalArgumentException.class)
187209
public void arithmenticProjectionOperationModByZeroException() {
188210

0 commit comments

Comments
 (0)