MongoDB made more Groovy
07 November, 2009
The session after mine here at RuPy was MongoDB presented by Mike Dirlof(@mdirlof). I looked at the Java examples yet they left a bit to be desired as they had the usual Java verbosity problems. One of the cooler lesser known things about Groovy is that Groovy can coerce objects to a specific interface. We use this alot for one-off WindowListerners interfaces and such. That same concept can be applied to the classes as well. We can take code from the Java MongoDB tutorial to make it more Groovy.
import com.mongodb.*
def m = new Mongo()
def db = m.getDB("mydb")
def coll = db.getCollection("testCollection")
coll.drop()
def doc = [name:"MongoDB", type:"database", count:1,
info: [x:203, y:102]
] as BasicDBObject
def doc2 = [name:"MongoDB2", type:"database", count:2,
info: [x:203, y:102] ] as BasicDBObject
coll.insert(doc)
coll.insert(doc2)
println coll.getCount()
def obj = coll.findOne([count:1] as BasicDBObject)
println obj
println "showing a custom query"
def cur = coll.find([count:['$lt':3]] as BasicDBObject)
while(cur.hasNext()) {
println cur.next()
}
Because MongoDB's BasicDBObject is a subclass of HashMap, we can produce concise code that looks closer to the Ruby, Python, and Javascript examples Matt has presented. We can also nest documents within documents, turtles all the way down. Only the othermost document needs to be cast, the rest get cast to documents automatically. It seems that MongoDB's special params begin with a dollar sign ($gt, $lt, etc) need to be passed as a String literal with apostrophes.
Groovy was not snubbed
Java does need an answer to Rails
Desklets, Java widgets
Hello World Groovy Desklet
Comments
- Tomash said: You should sooo make it a Groovy MongoDB driver (with Java driver as dependency) -- I'd love to have Grails capable of interfacing with MongoDB in a nice way (just put it in DataSource and let's roll)
- Mike Dirolf said: +1
- TDM said: Yes indeed, it would be really great :)
- Patrick B Haggood said: I still don't "get" MongoDB + Groovy - am I storing each of my groovy object's properties individually or am I storing only enough of the object for an index then a binary of the remainder of the object? class Customer { String name String phone byte[] image URL website } cust = new Customer() cust.name = blah; cust.phone = blah, cust.image = getImageFromFile(image); cust.URL = blah; def doc = [name:cust.name, URL:cust.URL,image:cust.image] as BasicDBObject def doc2 = [name:cust.name, customerBlob:cust] as BasicDBObject coll.insert(doc) coll.insert(doc2)
- Lindsay from Brisbane said: Finally, I find this complete explanation! Thanks a lot!
- Energy said: Lindsay - I just wanted to say that, no, really :) David
- fuel said: Yeah I had the same problem, only pieces of information, but no complete answer to my problem, its the weak point of internet research - you can find lot of crap before the right info, but at least I found it - thanks james williams :) JoHnV
- freind said: Never heard MongoDB before. So, I really thankful for your complete expalanation. Indeed, there many thinngs to learn from internet.
- Mark said: Hi James, I've recently started work on a mongodb grails plugin and someone on the MongoDB groups pointed me to your site. If you have any interest in collaborating, or if you have a more mature plugin in the works, let me know. The project and my contact info are up on github: http://github.com/mpriatel/mongodb-grails