skip to Main Content

I have a mongoDB Query that uses aggregation. I am applying projection and then unwind.

 ProjectionOperation projectionPreUnwind = Aggregation.project(Fields.from(Fields.field("result.detections.reference.control", "result.detections.reference.control")));

        UnwindOperation unwindOpn = Aggregation.unwind("result.detections");

        Aggregation aggregation = Aggregation.newAggregation(projectionPreUnwind, unwindOpn);

        LOGGER.info(" aggregation {}", aggregation);

        AggregationResults<String> detectionsDataAggr = mongoTemplate.aggregate(aggregation, CASES_COLLECTION, String.class);

The code result in Error

2024-10-23 16:48:00.097  INFO B33543D --- [nio-8082-exec-9] .c.c.s.d.r.c.i.MyRepoImpl : ENTERING   aggregation [!!!org.springframework.data.mongodb.core.aggregation.Aggregation@3fee8ffe=>java.lang.IllegalArgumentException:Invalid reference 'result.detections'!!!!]

2024-10-23 16:48:03.158 ERROR B33543D — [nio-8082-exec-9] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Invalid reference ‘result.detections’!] with root cause
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)

Sample JSON File

{
"result": {
    "aiModel": "",
    "detections": [
        {
            "aiStatus": "PASS",
            "reference": {
                "attribute": "TEST",
                "control": "C Test"
            }
        },
        {
            "aiStatus": "FAIL",
            "reference": {
                "attribute": "TES22T",
                "control": "C Test"
            }
        }
    ]
}

}

2

Answers


  1. Chosen as BEST ANSWER
    //    Using projection with this code, solved the issue.
    
    Document projectFields = new Document();
     projectFields.append("_id", 1);
     projectFields.append("result.detections.reference.control", 1);
     
     AggregationOperation projectOperation = context -> new Document("$project", projectFields);
    

  2. I think the problem is in ProjectOperation

    You are trying with same key and try to access value with same key and then you are trying to unwind that but its throw error because in unwind operation you can get proper reference of a key so review below code and give simple key in projection.

    I think you can try projection like this

    ProjectionOperation projectionOperation = Aggregation.project().and("YOUR_ACTUAL_FIELD_WHICH_ONLY_YOU_WANT").as("KEY_WHICH_YOU_DEFINE_TO_YOUR_VALUE");
    

    Then you should try unwind operation

    UnwindOperation unwindOpn = Aggregation.unwind("YOUR_PROJECTION_OUTPUT_KEY");
    

    I am also attaching the correct mongo query :

    https://mongoplayground.net/p/IPPFG-gr3uY

    Hope this will work for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search