2 Getting Started - Reference Documentation
Authors: Graeme Rocher, Burt Beckwith
Version: 1.1.0.GA
Table of Contents
2 Getting Started
To get started with GORM for Mongo you need to install the plugin into a Grails application:grails install-plugin mongodb
BuildConfig.groovy:plugins {
compile ':mongodb:1.0.0.GA'
}MONGO_HOME/bin/mongod
Thu Nov 11 16:54:08 MongoDB starting : pid=4600 port=27017 dbpath=/data/db/ 64-bit Thu Nov 11 16:54:08 db version v1.6.3, pdfile version 4.5 Thu Nov 11 16:54:08 git version: 278bd2ac2f2efbee556f32c13c1b6803224d1c01 Thu Nov 11 16:54:08 sys info: Darwin erh2.10gen.cc 9.6.0 Darwin Kernel Version 9.6.0: Mon Nov 24 17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_40 Thu Nov 11 16:54:08 [initandlisten] waiting for connections on port 27017 Thu Nov 11 16:54:08 [websvr] web admin interface listening on port 28017
grails-app/conf/DataSource.groovy:grails {
mongo {
host = "localhost"
port = 27017
username = "blah"
password = "blah"
databaseName = "foo"
}
}2.1 Using Mongo Standalone
If you plan to use Mongo as your primary datastore then you need to uninstall the Hibernate plugin:grails uninstall-plugin hibernate
grails-app/conf/BuildConfig.groovy file comment out the hibernate line in the plugins blockcompile ':hibernate:2.0.0'
create-domain-class command:grails create-domain-class Person
Person domain class will automatically be a persistent entity that can be stored in Mongo.
2.2 Combining Mongo and Hibernate
If you have both the Hibernate and Mongo plugins installed then by default all classes in thegrails-app/domain directory will be persisted by Hibernate and not Mongo. If you want to persist a particular domain class with Mongo then you must use the mapWith property in the domain class:static mapWith = "mongo"
mongo scope added to all Hibernate entities:def hibernatePerson = Person.get(1)hibernatePerson.mongo.save()def mongoPerson = Person.mongo.get(1)
2.3 Advanced Configuration
Mongo Database Connection Configuration
As mentioned the GORM for Mongo plugin will configure all the defaults for you, but if you wish to customize those defaults you can do so in the yourgrails-app/conf/DataSource.groovy file:grails {
mongo {
host = "localhost"
port = 27107
username = "blah"
password = "blah"
databaseName = "foo"
}
}databaseName setting configures the default database name. If not specified the databaseName will default to the name of your application.You can also customize the Mongo connection settings using an options block:grails {
mongo {
options {
autoConnectRetry = true
connectTimeout = 300
}
}
}grails {
mongo {
replicaPair = [ "localhost:27017", "localhost:27018"]
}
}grails {
mongo {
replicaSet = [ "localhost:27017", "localhost:27018"]
}
}Global Mapping Configuration
Using thegrails.mongo.default.mapping setting in Config.groovy you can configure global mapping options across your domain classes. This is useful if, for example, you want to disable optimistic locking globally or you wish to use DBRefs in your association mappings. For example, the following configuration will disable optimistic locking globally and use DBRefs for all properties:grails.mongo.default.mapping = { version false '*'(reference:true) }
* method is used to indicate that the setting applies to all properties.