0

My application is in grails 2.3.7 and I'm using mongodb as datastore. I have a use case where I need to use aggregation framework to fetch and filter data from the DB. I was able to do this easily using java mongodb driver. But how do I achieve the same using mongodb grails plugin? I'm able to fetch the data and apply criteria to filter out the results. But I'm unable to run an aggregation pipeline. Any clues?

coder
  • 1,901
  • 5
  • 29
  • 44

2 Answers2

0

mongo plugin of ver. 3.0.0 should have aggregation support. if you have some limitations, see how it's possible to enable aggregation in Grails pre-2.x version Aggregations in gmongo 0.9.1

Community
  • 1
  • 1
injecteer
  • 20,038
  • 4
  • 45
  • 89
  • Where are the docs for the 3.0.0 aggregation support? I just checked the docs and while they say it's included, but there is exactly zero information how the new feature works! So typical Grails Mongo plugin *facepalm* – chubbsondubs Jun 29 '15 at 19:34
  • the docs for aggregation support are to be found at the mongo site (where else?). In your groovy code you must use the "low-level notation" `YourDomain.collection.aggregate( [:], [:] )` – injecteer Jun 30 '15 at 08:09
  • By putting a reference to the "Support for projections using MongoDB aggregation" in their change log makes it sounds like they developed something above what GMongo already did. If that's really all there is then some details about what that really means is all that's needed (ie aggregation framework see GMongo) to clear up the confusion. And going to the MongoDB site to understand how a Groovy library should be used is flippant advice. The correct place is GMongo docs because I need to know how to call aggregation from Groovy not Javascript. – chubbsondubs Jun 30 '15 at 15:22
0

You do it exactly the same as you would do in Java. In a controller if you add a Mongo that is of type com.mongodb.Mongo field it will be automatically be injected, you then just use the same API as the driver to perform whatever aggregations you want:

Mongo mongo
def testMongo() {
   DB myDb = mongo.getDB("foo")
   // do stuff with DB
}
Graeme Rocher
  • 7,985
  • 2
  • 26
  • 37
  • Yes I'm doing this now. But I wanted to know if this results in getting DB connection for every request. Lets say I add this in a controller. I dont want a new DB connection to be fetched every time a request comes in – coder Jun 10 '14 at 16:20