spring-projects / spring-data-mongodb

Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-mongodb/
Apache License 2.0
1.61k stars 1.08k forks source link

Aggregation pipeline breaks if $replaceRoot stage is present #4722

Open jmayday opened 3 months ago

jmayday commented 3 months ago

This is similar to already closed https://github.com/spring-projects/spring-data-mongodb/issues/4285.

If I include $replaceRoot stage, aggregation fails with CodecConfigurationException. Below is the (bit anonymized) aggregation itself. Workaround is to use $project instead of $replaceRoot (which is not that convenient).

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for CodecCacheKey{clazz=class org.springframework.data.mongodb.core.aggregation.BooleanOperators$And, types=null}.
[
    {
        "$match": {
            "$and": [
                {
                    "fieldA": "some value"
                }
            ]
        }
    },
    {
        "$sort": {
            "updatedAt": -1
        }
    },
    {
        "$group": {
            "_id": "$_some_field_id",
            "latestRecord": {
                "$first": "$$ROOT"
            }
        }
    },
    {
        "$replaceRoot": {
            "newRoot": "$latestRecord"
        }
    },
    {
        "$lookup": {
            "from": "joinedCollectionName",
            "let": {
                "fieldB": "$fieldB",
                "fieldA": "$fieldA",
                "fieldC": "$fieldC"
            },
            "pipeline": [
                {
                    "$match": {
                        "$expr": {
                            "$java": org.springframework.data.mongodb.core.aggregation.BooleanOperators$And@5d77efc8
                        }
                    }
                }
            ],
            "as": "myAlias"
        }
    }
]

Some dependency versions:

[INFO] +- org.springframework.boot:spring-boot-starter-data-mongodb:jar:3.3.0:compile
[INFO] |  +- org.mongodb:mongodb-driver-sync:jar:5.0.1:compile
[INFO] |  |  +- org.mongodb:bson:jar:5.0.1:compile
[INFO] |  |  \- org.mongodb:mongodb-driver-core:jar:5.0.1:compile
[INFO] |  |     \- org.mongodb:bson-record-codec:jar:5.0.1:runtime
[INFO] |  \- org.springframework.data:spring-data-mongodb:jar:4.3.0:compile
[INFO] |     +- org.springframework:spring-tx:jar:6.1.8:compile
[INFO] |     \- org.springframework.data:spring-data-commons:jar:3.3.0:compile
christophstrobl commented 3 months ago

Do you happen to have a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem? Also the java representation of the pipeline as well as execution paths & stack traces would be a good thing to add. Thank you!

pbeltechi commented 3 months ago

@christophstrobl please check @banhidizoli's example from here https://github.com/spring-projects/spring-data-mongodb/issues/4285

I manage to reproduce it on 4.2.5 version.

moldo1997 commented 1 month ago

Hello,

Are there any updates regarding this issue?

Kind Regards, Cristian