Lately, I've been working pretty extensively with mongodb. I classify it as a "JIT DB", as in Just-In-Time Schema Database. It's perfect for lazy moments when you're writing some code and it dawns on you that you need an additional field or even an additional table (called "collections" in Mongo).
"Lazy" is the wrong word. mongoDB is in a class of technologies and strategies that foster inspired notions and reduce barriers (like time and patience) to assert your ideas. SQL doesn't do that for me. The level of required schema pre-work and retrofitting has nipped some cool ideas in the bud... mongoDB encourages me to do it right now because I don't see any impedance! Throw together shell scripting, Groovy and mongoDB and let's just do it!
Here's a quick example that will hopefully illustrate for you the low impedance of mongoDB (and lots of other unSQL databases)...
Let's sort a table called myData by a timestamp field.
db.mydata.find().sort({timeStamp:-1}).
This is equivalent to
select * from mydata order by timeStamp desc;
mongodb comes back and says something to the effect of "can't do a big sort like this without an index." Well there you go... so you type
db.mydata.ensureIndex({timeStamp:1})
You try the sort again and it works. You've just experienced something like a conversation with your database! "I can't do this... you know what to do..."
In full disclosure, I acutally use Groovy for all my Java-style development now. I've completely lost interest in Java because 1) Groovy is way more satisfying and productive and 2) I, currently, have no
need to use Java for squeezing max performance out of code. I mostly use Groovy for batch-style work, updating Salesforce.com via the Web Services API and such.
With mongoDB there's a nasty little conceptual hurdle to jump over, especially switching back and forth between using native javascript commands and Java driver programming.
In Java, there are at least 2, 3 or more ways to construct a db operation
def doc = new BasicDBObject().append("lastName","Smith").append("firstName","Jack")
myData.query(doc)
...versus...
def person = [:] // Groovy syntax
person.lastName = "Smith"
person.firstName= "Jack"
def doc = new BasicDBObject(person)
myData.query(doc)
Straightforward enough. In the native mongo language, the query looks something like this...
db.myData.find({lastName:"Smith",firstName:"Jack"})
which is equivalent to
select * from myData where lastName = 'Smith' and firstName = 'Jack';
Now, because I'm a Linux guy and I love the power of intermingling shell scripts to glue Java/Groovy together as needed, here's one way way you might integrate that mongoDB (javascript) script language in a bash shell script using a here document.
function findUser {
lastName=$1
firstName=$2
mongo <<EOF
use employeeDB
var criteria = {lastName:"${lastName}",firstName:"${firstName}"}
var answer = db.leads.find(criteria)
answer.count()
EOF
}
findUser Smith Joe
Enough for now. For the next few weeks, I'll post some of the mongodb commands and concepts I found most useful.
"Lazy" is the wrong word. mongoDB is in a class of technologies and strategies that foster inspired notions and reduce barriers (like time and patience) to assert your ideas. SQL doesn't do that for me. The level of required schema pre-work and retrofitting has nipped some cool ideas in the bud... mongoDB encourages me to do it right now because I don't see any impedance! Throw together shell scripting, Groovy and mongoDB and let's just do it!
Here's a quick example that will hopefully illustrate for you the low impedance of mongoDB (and lots of other unSQL databases)...
Let's sort a table called myData by a timestamp field.
db.mydata.find().sort({timeStamp:-1}).
This is equivalent to
select * from mydata order by timeStamp desc;
mongodb comes back and says something to the effect of "can't do a big sort like this without an index." Well there you go... so you type
db.mydata.ensureIndex({timeStamp:1})
You try the sort again and it works. You've just experienced something like a conversation with your database! "I can't do this... you know what to do..."
In full disclosure, I acutally use Groovy for all my Java-style development now. I've completely lost interest in Java because 1) Groovy is way more satisfying and productive and 2) I, currently, have no
need to use Java for squeezing max performance out of code. I mostly use Groovy for batch-style work, updating Salesforce.com via the Web Services API and such.
With mongoDB there's a nasty little conceptual hurdle to jump over, especially switching back and forth between using native javascript commands and Java driver programming.
In Java, there are at least 2, 3 or more ways to construct a db operation
def doc = new BasicDBObject().append("lastName","Smith").append("firstName","Jack")
myData.query(doc)
...versus...
def person = [:] // Groovy syntax
person.lastName = "Smith"
person.firstName= "Jack"
def doc = new BasicDBObject(person)
myData.query(doc)
Straightforward enough. In the native mongo language, the query looks something like this...
db.myData.find({lastName:"Smith",firstName:"Jack"})
which is equivalent to
select * from myData where lastName = 'Smith' and firstName = 'Jack';
Now, because I'm a Linux guy and I love the power of intermingling shell scripts to glue Java/Groovy together as needed, here's one way way you might integrate that mongoDB (javascript) script language in a bash shell script using a here document.
function findUser {
lastName=$1
firstName=$2
mongo <<EOF
use employeeDB
var criteria = {lastName:"${lastName}",firstName:"${firstName}"}
var answer = db.leads.find(criteria)
answer.count()
EOF
}
findUser Smith Joe
Enough for now. For the next few weeks, I'll post some of the mongodb commands and concepts I found most useful.
I (mostly) share your enthusiasm for switching to Groovy and I've started experimenting with mongodb ... it's fun and productive, though often I miss the blessings of the type info in raw Java or the preplanning and structure from using SQL :)
ReplyDeleteBut the idea of using a 'here' doc to push mongo ... that's nice, thanks!
Wayne, I'm elated that you found some value in what I came up with. After seeing your blog, I'm a bit intimidated. Awesome work! I hope Obama is reading it!
ReplyDelete