(Quick Reference)

2 Quickstart - Reference Documentation

Authors: Juri Kuehn

Version: 0.8.0

2 Quickstart

Here you will find a step by step guide to set up a test project that uses the mongodb-morphia plugin to manipulate domain classes in Grails

Creating a test project that uses mongodb-morphia

We are going to create a new Grails application that has only one domain class: Car. Using the generate-all command we will generate the controllers and views necessary to manipulate instances of that domain.

Let's start off, create a new Grails project and install the mongodb-morphia plugin:

> grails create-app MongoGrailsQuickstart
> cd MongoGrailsQuickstart
> grails install-plugin mongodb-morphia

The mongodb-morphia plugin provides a create-mongodb-class command that creates a stub for the domain class, as well as corresponding test cases. Let's call it:

> grails create-mongodb-class Car

MongoDB domain classes are put into the grails-app/mongo directory. Now we need to edit some files: Config.groovy for the database connection and our new Car domain, where we want to put some fields:

grails-app/conf/DataSource.groovy

mongodb {
  host = '192.168.1.36' // adjust this according to your settings
  port = 27017
  databaseName = 'test'
  username = 'user'  // database user and password, if server requires authentication
  password = 's3cret'
}

You can configure replica sets and override default MongoOptions this way:

mongodb {
  replicaSet = [ "localhost:27017", "localhost:27018"]
  databaseName = 'test'
  options = new MongoOptions(readPreference: ReadPreference.nearest()) // optional: configure MongoOptions here
}

Now the fields and constraints

grails-app/mongo/mongograilsquickstart/Car.groovy

package mongograilsquickstart

class Car { String name String brand

int ps // horsepower Date buildDate

Date dateCreated // autoset by plugin Date lastUpdated // autoset by plugin

static constraints = { brand nullable:true ps min: 30, max: 1001 } }

Now generate the controllers and views and fire up the app!

> grails generate-all mongograilsquickstart.Car
> grails run-app

Now point your browser to http://localhost:8080/MongoGrailsQuickstart Done!