Mongodb Aggregation framework for grails 1.3.7

感情迁移 提交于 2019-12-12 06:14:21

问题


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.


回答1:


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()]]



来源:https://stackoverflow.com/questions/14967971/mongodb-aggregation-framework-for-grails-1-3-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!