Beginning Grails 2.0 with MongoDB

This is the quick step-by-step tutorial about beginning Grails 2.0 with mongoDB(Quickstart) for newbies who have read the manual but got nothing.
First thing first, you must install mongoDB on your machine by following Qucikstart Guide. if you done, let’s go.

1. Create your Grails application as you like.

$ grails create-app sample

2. Install mongodb plugin.

$ grails install-plugin mongodb

You don’t have to worry about configuring plugin, that all be done by default.

3. Remove hibernate plugin if you need to use only mongodb to store all of data in your application, or not? skip this step.

$ grails uninstall-plugin hibernate

or just remove or comment this line of code

runtime ":hibernate:$grailsVersion"

in yourapp/grails-app/conf/BuildConfig.groovy

4. Create Domain class as you like.

$ grails create-domain-class sample.Person

Then edit your domain class by adding some properties to Person class like.

String firstname
String lastName

if you still have the hibernate plugin, you will need to add this line to your domain class.

static mapWith="mongo"

5. Generate controller with scaffolding.

$ grails generate-controller sample.Person

and edit controller class by adding def scaffold = Person and controller will look like this.

package sample
import org.springframework.dao.DataIntegrityViolationException
class PersonController {
def scaffold = Person

6. Run application, be for run application you have to be sure that mogod is running by

$ mongostat

if you got this message “couldn’t connect to [] couldn’t connect to server” you must start the mongod, see how to start mongod  but if the mongo is already running you will see the result of that command like this

After everything goes OK It’s time to run-app
$ grails run-app
and hit the browser by localhost:8080/yourapp you will see

7. Test insert and query data

8. Use mongoDB shell to see data.
$ mongo
MongoDB shell version: 2.0.2
connecting to: test
> show dbs;
local (empty)
sample 0.203125GB
> use sample;
switched to db sample
> show collections;
> db.person.find();
{ “_id” : NumberLong(1), “firstName” : “Suriya”, “lastName” : “Sripalang”, “version” : 0 }
{ “_id” : NumberLong(3), “firstName” : “Isaac”, “lastName” : “Newton”, “version” : 0 }
{ “_id” : NumberLong(4), “firstName” : “Mark”, “lastName” : “Viduka”, “version” : 0 }
{ “_id” : NumberLong(5), “firstName” : “Homer”, “lastName” : “Simpson”, “version” : 0 }

Yes! we did it. It’s so really easy to connect mongoDB with Grails 2.0
Need more? see Grails mongoDB manual. Talk to me @etcpe9 

1 thought on “Beginning Grails 2.0 with MongoDB

  1. TRUONG Ngoc Tuan

    Can you show me how to config DataSource for this project? I currently have a problem with DataSource. I think that you need at least tell system to access “sample” mongo database, is it right ?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s