DATAMONGO-2644 - ProjectOperation no longer errors on inclusion of default _id field.
Original pull request: #890.
This commit is contained in:
committed by
Mark Paluch
parent
c7e1ca5863
commit
07c728bb32
@@ -264,7 +264,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
|
||||
return new Document(getOperator(), fieldObject);
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#getOperator()
|
||||
*/
|
||||
@@ -1450,6 +1450,14 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
|
||||
return field.getTarget();
|
||||
}
|
||||
|
||||
if (field.getTarget().equals(Fields.UNDERSCORE_ID)) {
|
||||
try {
|
||||
return context.getReference(field).getReferenceValue();
|
||||
} catch (java.lang.IllegalArgumentException e) {
|
||||
return Fields.UNDERSCORE_ID_REF;
|
||||
}
|
||||
}
|
||||
|
||||
// check whether referenced field exists in the context
|
||||
return context.getReference(field).getReferenceValue();
|
||||
|
||||
|
||||
@@ -29,6 +29,11 @@ import org.bson.Document;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond;
|
||||
import org.springframework.data.mongodb.core.aggregation.ProjectionOperationUnitTests.BookWithFieldAnnotation;
|
||||
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
||||
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
|
||||
import org.springframework.data.mongodb.core.convert.QueryMapper;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
|
||||
/**
|
||||
@@ -582,6 +587,17 @@ public class AggregationUnitTests {
|
||||
"{\"attributeRecordArrays\": {\"$reduce\": {\"input\": \"$attributeRecordArrays\", \"initialValue\": [], \"in\": {\"$concatArrays\": [\"$$value\", \"$$this\"]}}}}"));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2644
|
||||
void projectOnIdIsAlwaysValid() {
|
||||
|
||||
MongoMappingContext mappingContext = new MongoMappingContext();
|
||||
Document target = new Aggregation(bucket("start"), project("_id")).toDocument("collection-1",
|
||||
new RelaxedTypeBasedAggregationOperationContext(BookWithFieldAnnotation.class, mappingContext,
|
||||
new QueryMapper(new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext))));
|
||||
|
||||
assertThat(extractPipelineElement(target, 1, "$project")).isEqualTo(Document.parse(" { \"_id\" : \"$_id\" }"));
|
||||
}
|
||||
|
||||
private Document extractPipelineElement(Document agg, int index, String operation) {
|
||||
|
||||
List<Document> pipeline = (List<Document>) agg.get("pipeline");
|
||||
|
||||
Reference in New Issue
Block a user