alanwilliamson

Integrating MongoDB with CFML:Java on OpenBD

Earlier, I wrote about the power of embedding Java straight into CFML pages, without any prior compilation or packaging. Just write Java and hit refresh on the browser.

This is a feature that I have found myself using more and more. The ability to simply throw Java snippets into a web page and not worry about all the housekeeping that goes with it has allowed me to do many cool things that are just natural to Java.

One of the bigger pieces though, was to write a very fast layer on-top of the MongoDB Java driver to give a very fast access to the wonders of the MongoDB server.

I created a CFML CFC that basically is a thin wrapper to a single <cfscript language="java"></cfscript>, which bridges the gap between the CFML and the MongoDB driver world. All the Java methods marked as public are exported to the CFML world as callable methods.

Using MongoDB from Java is somewhat clunky it has to be said. With its DBObject interface, building up queries and blocks of content can feel strained at times. One of the advantages of the CFML world, is the ability to express structures and arrays in a pure JSON markup. This makes transacting with MongoDB through CFML a complete breeze, with the underlying mongo.cfc Java component doing all the translations behind the scenes for you.

This makes writing code to interface to MongoDB extremely quickly as you can see here:

<cfset application.m = new mongo()>
<cfset application.m.setup("127.0.0.1", "mydb")>

<cfloop index="x" from="1" to="100">

  <cfset application.m.insert( "mycollection", {
     name:"Document" & x,
     age:x,
     children : {
       child1 : "name1",
       child2 : "name2",
       child3 : "name3"
     }
   } )>

</cfloop>

One of the biggest advantages of going through here, is that you gain all the power, speed and efficiency of the native Java driver, without having to go through a whole host of inefficient layers that I have seen other CFML components attempt.

You can read about all the specific details and download the completed CFC here:

http://www.openbluedragon.org/manual/?/database_mongodb


 

Recent Cloud posts

Recent JAVA posts

Latest CFML posts


 
Site Links