gorm-mongodb icon indicating copy to clipboard operation
gorm-mongodb copied to clipboard

aggregate GroovyCastException in version 7.1.0

Open miguelabautista opened this issue 3 years ago • 5 comments

Environment:

Grails 5 GORM 7.1.0 Java 8

Problem:

I'm trying to run this simple code:

collection.aggregate(
        Arrays.asList(
                Aggregates.match(Filters.eq("categories", "Bakery")),
                Aggregates.group('$stars', Accumulators.sum("count", 1))
        )
).forEach(doc -> System.out.println(doc.toJson()));

the system throws this exception:

Cannot cast object 'Stage{name='$match', value=Filter{fieldName='categories', value=Bakery}}' with class 'com.mongodb.client.model.Aggregates$SimplePipelineStage' to class 'java.util.Map'. 

NOTE: Just tested in an empty gradle project with the same mongodb-driver-sync version and it works. and in previous grails version (4.0.12) this code works

why Is grails forcing me to use Map syntax?

miguelabautista avatar Oct 18 '21 14:10 miguelabautista

the problem is this class: MongoExtensions

if I do this the code works:

api('org.grails.plugins:mongodb:7.1.0') {
       exclude module: "grails-datastore-gorm-mongodb-ext"
}

miguelabautista avatar Oct 25 '21 21:10 miguelabautista

Please attach a sample application.

puneetbehl avatar Nov 03 '21 04:11 puneetbehl

TestAggregate.zip

@puneetbehl created a simple project

miguelabautista avatar Nov 03 '21 10:11 miguelabautista

adding .collect { it.toBsonDocument() } now it works:

collection.aggregate( Arrays.asList( Aggregates.match(Filters.eq("categories", "Bakery")), Aggregates.group('$stars', Accumulators.sum("count", 1)) ).collect { it.toBsonDocument() } )

I discovered that last night while migrating the queries from Aggregates to map syntax. but honestly I prefer not to.

miguelabautista avatar Dec 17 '21 21:12 miguelabautista

Just to bump this, I spent the morning trying to figure out this same issue with 7.3.0.

paulfairless avatar May 31 '23 12:05 paulfairless