Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aggregate GroovyCastException in version 7.1.0 #353

Open
miguelabautista opened this issue Oct 18, 2021 · 5 comments
Open

aggregate GroovyCastException in version 7.1.0 #353

miguelabautista opened this issue Oct 18, 2021 · 5 comments

Comments

@miguelabautista
Copy link
Contributor

miguelabautista commented Oct 18, 2021

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
Copy link
Contributor Author

miguelabautista commented Oct 25, 2021

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"
}

@puneetbehl
Copy link
Contributor

Please attach a sample application.

@miguelabautista
Copy link
Contributor Author

miguelabautista commented Nov 3, 2021

TestAggregate.zip

@puneetbehl created a simple project

@miguelabautista
Copy link
Contributor Author

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.

@paulfairless
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants