Halley's Blog

Using Grails with MongoDB

Posted in Grails, Technical by Halley on January 20, 2013

Recently, I gave a talk at the JUDCon 2013 about how we can push a Grails application to the cloud and use MongoDB as the back end. For that, I did some research on the internet and found that Grails has a plugin to connect to MongoDB and do the persistence. Essentially, the plugin replaces the default Hibernate persistence layer and provide an object mapping layer on top of Mongo.

The interesting thing about the plugin is that, it opens a host of new opportunities for a Grails developer to leverage on the full potential of Mongo’s schemaless world.

Installing the MongoDB plugin

Let’s first take a look at how we can install the plugin for our Grails application.

grails install-plugin mongodb

This command will install the plugin. Interestingly, the plugin is designed to work out of the box with zero configurations required. It assumes your MongoDB server is running on localhost with port number 27107 and creates a database with the name of you application and uses it. You can add the settings in the DataConfig.groovy file as follows.

grails {
mongo {
host = "localhost"
port = 27107
username = "user"
password="password"
databaseName = "doctors"
}
}

The plugin is compatible with the default Hibernate plugin GROM uses. But, if you are planning to use Mongo as your primary database, then you must uninstall the hibernate plugin using the command

grails plugin-uninstall hibernate

That’s it. Now, you can continue your application developement and all the data will be persisted into your Mongo database.

If you are planning to use Mongo along with a relational databse, then do not uninstall the hibernate plugin. Instead, whenever you need a domain class to be persisted by the Mongo plugin, then use the mapWith property in the class as follows

static mapWith="mongo"

GORM and Mongo

Even though GORM was designed to use with a relational database, most of its powerful features are still available with Mongo also.

  • Basic list() and get() methods
  • Dynamic finders
  • Criteria queries

Dynamic Grails and the Schemaless Mongo

The exciting part about the plugin is where it leverages the schemaless feature of Mongo using the dynamic aspects of Groovy and Grails. To practically explore it, we will be considering a domain class as follows.

class User  {
  String name
  String email
  String dept
  }

Suppose you want to add a new property to your class called deliveryUnit. Now, if this is in a traditional SQL system, its a complete mess. New table, foreign keys, joins.. You name it!!! But in the schemaless world of NoSQL, its as simple as adding a new entry to the document. And, with Grails, its even simpler.

def u=new User(name: 'Foo',email:'foo@bar.com'dept:'Baz')

//Add deliveryUnit dynamically for this user.
u["deliveryUnit"]="Qux"

//Save the instance
u.save()

Now, if you query your MongoDB db for the user, you will find that this user instance will have one more property called deliveryUnit. No sweat.

One more interesting aspect of the plugin is that, dynamic finders will work even for dynamically added fields.┬áSo, the following code will work even if the original domain class didn’t have this property.

def u=User.findByDeliveryUnit()

As you can see, Groovy and Grails are perfectly suited for leveraging the schemaless Mongo database than other ORMs available.

Watch this space for more.

Reference links:

  1. GORM for Mongo – Reference Documentation
  2. MongoDB plugin page
Advertisements