How can I use Aggregation framework in Grails 1.3.7. At the moment i can't migrate into new version of grails. I have tried grails mongodb plugin 1.0.0.GA but it is using old java driver and gmongo libs. I have also tried to add dependencies for new libs/jars in build-config.groovy but still it is giving me error at runtime for aggregate method. Any help is highly appreciated.
In your BuildConfig.groovy put this
dependencies {
compile "org.mongodb:mongo-java-driver:2.10.1"
runtime "com.gmongo:gmongo:1.1"
}
And then in plugin section
plugins {
compile (":mongodb:1.1.0.GA"){
excludes 'mongo-java-driver', 'gmongo'
}
}
This will update your mongodb plugin to use the latest java drivers and gmongo.
Then use aggregation framework. Example
DBObject match = new BasicDBObject('$match', new BasicDBObject("adPostId", 50) );
// build the $projection operation
DBObject fields = new BasicDBObject("adPostId", 1);
fields.put("shopperId", 1);
fields.put("_id", 0);
DBObject project = new BasicDBObject('$project', fields );
// Now the $group operation
DBObject groupFields = new BasicDBObject( "_id", '$karmaType');
groupFields.put("average", new BasicDBObject( '$sum', '$rating'));
DBObject group = new BasicDBObject('$group', groupFields);
// run aggregation
AggregationOutput output = db.karma.aggregate( match, project, group );
return [model:[avgkarma:output.getCommandResult()]]