gorm-mongodb
gorm-mongodb copied to clipboard
aggregate GroovyCastException in version 7.1.0
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?
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"
}
Please attach a sample application.
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.
Just to bump this, I spent the morning trying to figure out this same issue with 7.3.0.