For a small presentation I am putting together, I started playing with MongoDB and Groovy.  The two work decently together but the documentation out there is really sparse.

First, drop your mongodb jar file into your groovy lib directory (If desired globally).

Then create some import statements.

import com.mongodb.Mongo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;

Then I defined a few helper closures for timing and to print results:

def benchmark = { closure ->
  start = System.currentTimeMillis()
  now = System.currentTimeMillis()
  now - start

def printResults = { cur ->
	if (cur) {
		while(cur.hasNext()) {

Get a reference to the database and open up a collection (dropping if it exists).

Mongo m = new Mongo();
DB db = m.getDB( "testdb" );

DBCollection custs = db.getCollection("customers")


Next, we can build out out schema/database.  In this example I want the following:

  • Customer
    • Phones
    • Addresses
def tranCnt = 100
def duration = benchmark {
	for (id in 1..tranCnt) {
		def random = new Random()
		def rand = random.nextInt(50) + 25
		def addresses = []
		for (itemId in 1..5) {
			def address = ["address1":itemId+id+" Test Street",
			addresses.add address
		def cust = ["party_id":id,
				"firstName":"Alex #"+id,
				"addresses": addresses,
				"insert_ts": new Date()] as BasicDBObject
custs.createIndex(new BasicDBObject("party_id", 1));
custs.createIndex(new BasicDBObject("addresses.address1", 1));
println "Created $tranCnt transactions in $duration ms ("+duration/(tranCnt/100)+" ms per 100 insert)"

Example query to pull out a customer with a party_id of 71:

BasicDBObject query = new BasicDBObject();
query.put("party_id", 71);
duration = benchmark {
	cur = custs.find(query)

Example to pull out items with party_id’s greater than or equal to 90 and less than 95.

  • $gt = Greater than
  • $gte = Greater than or equal to
  • $lt = Less than
  • $lte – Less than or equal to
  • There are many more examples on the mongodb site.
duration = benchmark {
	cur = custs.find([party_id:['$gte':90, '$lt':95]] as BasicDBObject)
println "Found #"+cur.count()+" customers in $duration ms."

A really powerful feature is using a regular expression to find data.
In this example, I am looking for customers with “Alex #” then a number following from 90 to 95:

duration = benchmark {
	cur = custs.find(["firstName" : ~/Alex #9[0-5]$.*/] as BasicDBObject)
println "Found #"+cur.count()+" items in $duration ms"

Next, I want to look into the objects and find all the customers with an address starting with a 1.

duration = benchmark {
	cur = custs.find(["addresses.address1" : ~/^1.*$/] as BasicDBObject, ["addresses.address1", 1] as BasicDBObject)

So this was a pretty quick example, but it gives you an idea of some of the power of MongoDB. For this example, I created 10,000 customer objects in 718ms (About 7.18ms per 100) on a workstation.