Tuesday, August 14, 2012

Grails and MongoDb

These are a few notes I took while setting up a project using grails 2.1.0 and mongodb 2.0.6.

Firstly there is a grails plugin for mongoDB. The notes for its use are here

So you can run grails install-plugin mongoDB (or update the BuildConfig.groovy as is the prefered method for grails guru's).

If you want Mongo to be your main DB, then you need to uninstall the hibernate plugin. This shoudl be done via the BuildConfig.groovy. (Comment out the //runtime ":hibernate:$grailsVersion" line)

Next you also need to remove the database-migration plugin, since this is uses the hibernate plugin (//runtime ":database-migration:1.1")

My mongoDB was running on port 27017 which is different from the post specified in the grails mongoDb tutorial (27107), so this must be updated the Datasource.groovy

After a bit of searching I found this sample MongoDb data file . (See http://stackoverflow.com/questions/5723896/is-there-a-sample-mongodb-database-along-the-lines-of-world-for-mysql)

To create a grails domain to fit in with the zips.bson data I did the following. Note we want the id field to be mapped to the mongo _id field, and since it represents the zip code we need to mark the generator as assigned.

class Zip {
    String city
    List loc
    Integer pop
    String state
    String id
   
   
    static mapping = {
        id generator: 'assigned',
        column: '_id'
    }

    static constraints = {
    }
}