I've been tinkering late at night with MongoDB and CouchDB, and honestly, I have very mixed feelings.
The end of the relational corset
For years, we've been slaves to the schema. If you remember when we talked about breaking Third Normal Form to gain performance, you'll know that the traditional relational model is sometimes a headache when you want to scale or when requirements change every Tuesday.
The promise of NoSQL is simple: throw the schema out the window and store data in document format (usually JSON or BSON). There are no tables, no rigid columns, and the most terrifying part for purists... there are no JOINs.
To test it, I downloaded the MongoDB 1.2 binaries, spun it up on my local machine, and started coding a small Python script using the pymongo library.
A practical example: Saving data without overthinking
Imagine we are saving user profiles. In MySQL we would have a users table, another for phones, and another for addresses, and we'd make a mess of foreign keys. In Mongo, you simply throw a Python dictionary at it and it takes care of the rest.
import pymongo
from pymongo import Connection
# Connect to the local server on the default port
conexion = Connection("localhost", 27017)
db = conexion.mi_proyecto
coleccion = db.usuarios
# We create a JSON document on the fly.
# Notice that one user has Twitter and the other doesn't. No problem.
usuario_1 = {
"nombre": "Carlos",
"edad": 28,
"habilidades": ["Python", "C", "MySQL"],
"contacto": {
"email": "carlos@ejemplo.com",
"twitter": "@carlos_dev"
}
}
usuario_2 = {
"nombre": "Ana",
"edad": 25,
"habilidades": ["PHP", "JavaScript"],
"contacto": {
"email": "ana@ejemplo.com"
} # No twitter here, Mongo couldn't care less.
}
coleccion.insert(usuario_1)
coleccion.insert(usuario_2)
# Find everyone who knows Python
for u in coleccion.find({"habilidades": "Python"}):
print "Encontrado:", u["nombre"]
It's ridiculously easy. I didn't have to define the structure beforehand. If tomorrow I decide I'm also going to save a "favorite color" for new users, I simply add it in the code and that's it. Mongo doesn't complain. And since it saves the data in a continuous binary format (BSON), reading is incredibly fast.
I've also kept an eye on CouchDB, which uses a native REST API over HTTP, which seems like wonderful madness to interact directly with JavaScript from the client, but its view system based on MapReduce in JavaScript seems a bit esoteric to me right now compared to classic queries.
Is this the future or a hipster fad?
I see lights and shadows. On one hand, the development speed is brutal. For a prototype or a web app where data doesn't have much structure (like a social network feed or a log system), it seems like a spectacular tool.
On the other hand, the lack of complex ACID transactions terrifies me. If I'm coding billing software for a bank or a high-concurrency shopping cart, I want my transactions, my row-level locking, and my relationships secured at the engine level, not at the application code level.
I think MySQL and PostgreSQL still have a long way to go. NoSQL databases aren't going to kill the relational model; they are going to coexist. We'll use MySQL for the money and MongoDB for massive unstructured data. We'll see if time proves me right or if in five years we're all writing queries in JSON and have forgotten about the SELECT.