问题
I need to implement some simple aggregations in my app powered by Grails 1.3.7. The mongodb-plugin of 1.0.0.RC3 ships with gmongo 0.9.1, where the aggregate functions are not implemented.
How can I solve the problem? Are there any hooks to call java-mongo API directly, or maybe there's some other plugin releases which allow aggregations?
TIA
回答1:
It seems that Mongo aggregation apis exist since 2.1 here, probably you might need to upgrade your libraries. Here is the mongodb plugin documentation which is talking about accessing low-level api. For grails 1.3.7 take at this blog for details on how to add more recent mongo libs into your Grails application and this post seems to have the same issue.
Hope it helps.
回答2:
Aggregations only work in GMongo 1.0+.
回答3:
Well, it seems to be impossible to do it with the existing gmongo/mongo-GORM. There are too many version clashes: different mongo java drivers, different groovy versions etc. I saw many ClassNotFoundExceptions and alike.
Luckily I don't need the aggregation functionality right now, so I'll just wait and upgrade to grails 2.x and mongo-GORM 1.3++ later
回答4:
So, I made it!
with a little amount of the blood shed, I found a way to use aggregations in gmongo 0.9.1 / mongodb 1.0.0.RC3 / Grails 1.3.7!
HOWTO:
you need to replace the mongo-java-driver with a newer version (I used the most recent for now 2.9.3). In Grails it looks like:
dependencies { compile 'org.mongodb:mongo-java-driver:2.9.3' }
In BootStrap or in my case Plugin-descriptor add the following line:
DBCollectionPatcher.PATCHED_METHODS << 'aggregate'
The aggregation invocation looks like:
def res = Task.collection.aggregate( [ $group:[ _id:'totalTime', time:[ $sum:'$time' ] ] ], [] as DBObject ).results()
and it works like a charm!
来源:https://stackoverflow.com/questions/17907002/aggregations-in-gmongo-0-9-1