8
Using MongoDB with Groovy James Williams (@ecspike) BT/Ribbit

Using MongoDB With Groovy

Embed Size (px)

DESCRIPTION

Lightning talk showing how to make MongoDB more Groovy Given at NoSQL Live Boston March 11,2010

Citation preview

Page 1: Using MongoDB With Groovy

Using MongoDB with GroovyJames Williams (@ecspike)BT/Ribbit

Page 2: Using MongoDB With Groovy

Just as NoSQL is...

• post - relational• schemaless• flexible

Page 3: Using MongoDB With Groovy

Groovy is ...

• a superset of Java• open to metaprogramming• a more concise way to write code

Page 4: Using MongoDB With Groovy

Groovy + Mongo

• JavaBasicDBObject doc = new BasicDBObject();doc.put("name", "MongoDB");doc.put("type", "database");doc.put("count", 1);coll.insert(doc);

• Groovierdef doc = [name:"MongoDB", count:1,type:"database", info: [x:203, y:102]          ] as BasicDBObjectcoll.insert(doc)

• Grooviest (using groovy-mongo)coll.insert([name:"MongoDB", type:"database", info: [x:203, y:102]])

Page 5: Using MongoDB With Groovy

Dynamic Finders

• let you query the database as if there was a schema• build complex query functions at runtime

        Ex. collection.findByAuthorAndPostCreatedGreaterThan(...)         collection.findByComments_CreatedOn(...)

Page 6: Using MongoDB With Groovy

How dynamic finders work

1. Groovy tries to execute the function but can't find it2. It invokes methodMissing instead3. The body of methodMissing takes the attempted method

name and parses it4. It uses the names and the modifiers (>, <, etc) with

BasicDBObjectBuilder to create a prototype5. It caches the created function under that name and runs it. 6. Further invocations don't incur the performance hit.

Page 7: Using MongoDB With Groovy

Groovy + Mongo in the wild

Page 8: Using MongoDB With Groovy

Links

• Andrill: http://andrill.org• CoreRef: http://coreref.org• groovy-mongo: http://github.com/jwill/groovy-mongo

• Personal Blog: http://jameswilliams.be/blog• Twitter: @ecspike