First off all you could find out some plugins https://grails.org/plugins/search?q=mongo

But if you need you could use another method:

1. Edit BuildConfig.groovy
add string like this: compile ‘org.mongodb:mongo-java-driver:3.0.3’ to dependencies section

dependencies {
        //...
	compile 'org.mongodb:mongo-java-driver:3.0.3'
    }

2. Edit Config.groovy

mongodb.uri = "mongodb://username:password@your.mongodb.server/yourDataBaseName"
mongodb.database = "yourDataBaseName"

3. Add to resources.groovy

beans = {
	mongoClientURI(MongoClientURI) { bean ->
		bean.constructorArgs = [grailsApplication.config.mongodb.uri.toString()]
	}
	mongoClient(MongoClient) { bean ->
		bean.constructorArgs = [ref(mongoClientURI)]
		bean.destroyMethod = 'close'
	}
	mongoDb(mongoClient: 'getDB') { bean ->
		bean.constructorArgs = [grailsApplication.config.mongodb.database.toString()]
	}
}

4. Add to any controller (in my case MyController)

package mypackage

import com.mongodb.DB

class MyController {

	DB mongoDb

	def users() {

		def collection = mongoDb.getCollection('myCollection')

		collection.insert(new BasicDBObject(['name':'Piter']))
		
		def users = collection.find(new BasicDBObject(['name':'Piter'])).toArray()
		
		render (contentType: 'application/json', text: JSON.serialize(users))
	}

	def user() {

		def userId = params.userId
		
		def collection = mongoDb.getCollection('myCollection')
		
		def user = collection.findOne(new BasicDBObject(['_id': new ObjectId(userId)]))
		
		render (contentType: 'application/json', text: JSON.serialize(user))
	}

}

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

required