1

I'm using the following aggregation pipepline and I'm having a weird behaviour in Spring Data Mongodb. The Aggregation is going well until the facet stage that throws Invalid reference 'producer.fundings.type'! exception.

Fields fieldsToBeGroupedTest = Fields.fields()
        .and("producerId", "producer.producerId")
        .and("name", "producer.name")
        .and("fundings", "producer.fundings");
GroupOperation groupOperationTest = group(fieldsToBeGroupedTest);

The result is projected according to

ProjectionOperation projectionOperationTest = project()
        .and("_id.producerId").as("producer.producerId")
        .and("_id.name").as("producer.name")
        .and("_id.fundings").as("producer.fundings")
        .andExclude("_id");

From the DEBUG logging, the following aggregation is executed by the database

[{
    "$group": {
        "_id": {
            "producerId": "$producer.producerId",
            "name": "$producer.name",
            "fundings": "$producer.fundings"
        }
    }
}, {
    "$project": {
        "producer.producerId": "$_id.producerId",
        "producer.name": "$_id.name",
        "producer.fundings": "$_id.fundings",
        "_id": 0
    }
}]

Several documents with the following schema are returned when this aggregation is executed in MongoShell:

{
    "producer": {
        "producerId": "CRYO",
        "name": [{
            "lang": "en",
            "text": "CRYOBS-CLIM"
        }],
        "fundings": [{
                "type": "Organisation",
                "acronym": "IRD",
                "name": [{
                    "lang": "en",
                    "text": "Institut de Recherche pour le Développement"
                }]
            },
            {
                "type": "Organisation",
                "acronym": "CNRS",
                "name": [{
                    "lang": "en",
                    "text": "Centre National de la Recherche Scientifique"
                }]
            }
        ]
    }
}

At the last stage of the pipeline the following FacetOperation is executed and return the exeception as if the unwind operation was not done.

FacetOperation facetOperationTest = facet(
        unwind("producer.fundings"),
        project().and("producer.fundings.type").as("type").and("producer.fundings.acronym").as("name"),
        group("name", "type").count().as("count"),
        project("count").and("_id.name").as("name").and("_id.type").as("type").andExclude("_id")
).as("fundingAcronymsFacet");

This FacetOperation works like a charm if executed in MongoShell as:

{
    "$facet": {
        "fundingAcronymsFacet": [{
            "$unwind": "$producer.fundings"
        }, {
            "$project": {
                "type": "$producer.fundings.type",
                "name": "$producer.fundings.acronym"
            }
        }, {
            "$group": {
                "_id": {
                    "name": "$name",
                    "type": "$type"
                },
                "count": {
                    "$sum": 1
                }
            }
        }, {
            "$project": {
                "count": 1,
                "name": "$_id.name",
                "type": "$_id.type",
                "_id": 0
            }
        }]
    }
}

Anyone already have encountered this unexpected behaviour?

charlycou
  • 1,778
  • 13
  • 37

0 Answers0