Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Using MongoDB With Groovy


Published on

Lightning talk showing how to make MongoDB more Groovy

Given at NoSQL Live Boston
March 11,2010

Published in: Technology
  • For more info on Groovy metaprogramming, check out: [PDF]
    Are you sure you want to  Yes  No
    Your message goes here

Using MongoDB With Groovy

  1. 1. Using MongoDB with Groovy James Williams (@ecspike) BT/Ribbit
  2. 2. Just as NoSQL is... <ul><ul><li>post - relational </li></ul></ul><ul><ul><li>schemaless </li></ul></ul><ul><ul><li>flexible </li></ul></ul>
  3. 3. Groovy is ... <ul><ul><li>a superset of Java </li></ul></ul><ul><ul><li>open to metaprogramming </li></ul></ul><ul><ul><li>a more concise way to write code </li></ul></ul>
  4. 4. Groovy + Mongo <ul><ul><li>Java </li></ul></ul><ul><li>BasicDBObject doc = new BasicDBObject(); </li></ul><ul><li>doc.put(&quot;name&quot;, &quot;MongoDB&quot;); </li></ul><ul><li>doc.put(&quot;type&quot;, &quot;database&quot;); </li></ul><ul><li>doc.put(&quot;count&quot;, 1); </li></ul><ul><li>coll.insert(doc); </li></ul><ul><ul><li>Groovier </li></ul></ul><ul><li>def doc = [name:&quot;MongoDB&quot;, count:1,type:&quot;database&quot;, info: [x:203, y:102] </li></ul><ul><li>           ] as BasicDBObject </li></ul><ul><li>coll.insert(doc) </li></ul><ul><ul><li>Grooviest (using groovy-mongo) </li></ul></ul><ul><li>coll.insert([name:&quot;MongoDB&quot;, type:&quot;database&quot;, info: [x:203, y:102]]) </li></ul>
  5. 5. Dynamic Finders <ul><ul><li>let you query the database as if there was a schema </li></ul></ul><ul><ul><li>build complex query functions at runtime </li></ul></ul><ul><li>         Ex. collection.findByAuthorAndPostCreatedGreaterThan(...) </li></ul><ul><li>          collection.findByComments_CreatedOn(...) </li></ul>
  6. 6. How dynamic finders work <ul><ul><li>Groovy tries to execute the function but can't find it </li></ul></ul><ul><ul><li>It invokes methodMissing instead </li></ul></ul><ul><ul><li>The body of methodMissing takes the attempted method name and parses it </li></ul></ul><ul><ul><li>It uses the names and the modifiers (>, <, etc) with BasicDBObjectBuilder to create a prototype </li></ul></ul><ul><ul><li>It caches the created function under that name and runs it.  </li></ul></ul><ul><ul><li>Further invocations don't incur the performance hit. </li></ul></ul>
  7. 7. Groovy + Mongo in the wild
  8. 8. Links <ul><ul><li>Andrill: </li></ul></ul><ul><ul><li>CoreRef: </li></ul></ul><ul><ul><li>groovy-mongo: </li></ul></ul><ul><ul><li>Personal Blog: </li></ul></ul><ul><ul><li>Twitter: @ecspike </li></ul></ul>