Sunday, April 14, 2013

MongoDB and Java

In the past few weeks I've participated at the M101J: MongoDB for Java Developers Course from 10gen Education. For those who are not familiar, MongoDB is an NoSQL database that's been very prominent amongst the "new" databases that have showed up after the NoSQL movement started.

I say "new" in quotes because most of them have been around for a long time, but only recently began to be considered real alternatives for production systems. I'd say that the industry has finally realized that polyglot persistence is a really obvious best practice and I'm amazed by how long it took for it to realize it.

Learning MongoDB is really fast so that in less than a day you can install it and begin inserting and querying data on your development machine. Of course, as any industry level database it will take longer for you to feel comfortable and productive with it, but it's mainly a convention over configuration tool and with impressively few steps you will have it up and running.

A few of the most important concepts used by MongoDB:
  • A document based database: it stores data in the form of whole documents instead of the traditional relational rows in a table. The documents are stored in the BSON format, which is a binary representation of JSON documents

  • It's schemaless: it does not enforce a determined pre-defined format that data has to have in order to be inserted. It means that you can basically put anything inside a collection (analogous to a table in a RDBMS) and it will be stored successfully by MongoDB. As cool as it might seem at first, this does require an application able to deal with that condition. Look at the following example:


  • Its supports indexes: as relational databases, MongoDB relies on indexes to return query results quickly. Indexes work pretty much the same here, they take up space, might slow your inserts and updates, but greatly decrease read times. The main difference is that on MongoDB you can create a Multi-Key Index, that is an index created on an array field. By the way, have you noticed on the example above that Mongo is able to store arrays? How great is that?
  • It's distributed: MongoDB can natively be distributed in two ways with Sharding or Replica Sets. Sharding is basically splitting up your collections on different physical servers to scale up horizontally. Replica Sets grant reliability and availability by using other server as backups to a database.

Using Java and MongoDB is pretty easy, use the MongoDB Java Language Center. The Language Center has a Java Driver provided by 10gen, which is just great and pretty easy to use. Here's a simple read code example:

This is just a simple read on a collection, to get a better grasp of what's really going on it's important to study the MongoDB client syntax, as the Driver relies on that and you can even send direct commands to the database. If you want less boilerplate code try the SpringData MongoDB integration
 
Learning MongoDB is a very rewarding experience for a Java developer. It's quick and very little messy, so that you can at least test it for all kinds of applications. It's reliable and scalable, so even if you got a project that's going to turn big, it's still a good idea. For those willing to dig deeper I really do recommend the courses available on 10gen Education. They've been developed with a lot of care by the company that made and maintains MongoDB. Also, they're free, so if you don't like it you can always drop it without regrets.