I am trying to create query using Projection & Unwind Operation to fetch necessary data which we want to process further.
ProjectionOperation initialProjection = Aggregation.project()
.and(“_id”).as(“_id”)
.and(“correlationId”).as(“correlationId”)
.and(“result.detections”).as(“detections”)
UnwindOperation unwindOpn = Aggregation.unwind(“result.detections”);
List postCriteriaList = new ArrayList<>();
postCriteriaList.add(new Criteria().andOperator(
Criteria.where(“result.detections.pictureName”).exists(true),
Criteria.where(“result.detections.pictureName”).ne(“”)
));
Criteria postMatchCriteria = new Criteria().andOperator(postCriteriaList);
MatchOperation postMatch = Aggregation.match(postMatchCriteria);
Aggregation aggregation = Aggregation.newAggregation(
preMatch,
unwindOpn,
projection,
postMatch)
AggregationResults<DetectionsUnwind> detectionsDataAggr = mongoTemplate.aggregate(aggregation, CASES_COLLECTION, DetectionsUnwind.class);
DetectionsUnwind class contains all the necessary fields which we want to display as output.
Now using this code we are getting error -
java.lang.IllegalArgumentException: Invalid reference 'result.detections'!
at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:114)
at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:77)
at org.springframework.data.mongodb.core.aggregation.UnwindOperation.toDocument(UnwindOperation.java:95)
at org.springframework.data.mongodb.core.aggregation.AggregationOperation.toPipelineStages(AggregationOperation.java:55)
at org.springframework.data.mongodb.core.aggregation.AggregationOperationRenderer.toDocument(AggregationOperationRenderer.java:56)
at org.springframework.data.mongodb.core.aggregation.AggregationPipeline.toDocuments(AggregationPipeline.java:81)
at org.springframework.data.mongodb.core.aggregation.Aggregation.toPipeline(Aggregation.java:716)
at org.springframework.data.mongodb.core.AggregationUtil.createPipeline(AggregationUtil.java:112)
at org.springframework.data.mongodb.core.MongoTemplate.doAggregate(MongoTemplate.java:2165)
at org.springframework.data.mongodb.core.MongoTemplate.doAggregate(MongoTemplate.java:2140)
at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:2134)
at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:2035)