MongoDB: How it compares to RDB

01 Mar 2012

I first heard about MongoDB on Devoxx 2011, lots of people were talking about it so I decided to take a look. I was quite amazed about the concept and will use it in most of my future projects. Not because MySQL is not appropriate, but because MongoDB can do the job a lot better. Find out why in this post.

Document oriented database

MongoDB is a document oriented storage. A document oriented storage is a concept where one can store documents (encoded en encapsulated data like XML, JSON, BSON) into a database. Unlike relational databases you do not need to define a schema to store objects. With a relational database you need to define a table with fields. If the field of a record is not used, it is still there but empty. A document-oriented database does not store empty values. It uses a unique key to represent a document. A key can be a string, URI or a path.

About MongoDB

MongoDB is an open source project. They focus on:
  • flexibility
  • speed
  • power
  • ease of use
They didn't want to make something completely new, they focussed on what wasn't working with the current RDB systems and wanted to fix those problems. They tackled them by moving to a document-oriented data model. This makes defining complex hierarchical relationships a lot easier. This is ideal when developers are thinking object-oriented.

Scaling

Something else they wanted to do was provide easy scaling. A common problem with RDB is when your datastore starts growing. Sometimes you notice that your schema is not really that great afteral when you need to expand, replicate and cluster your systems. Some people change the schema, but this is time consuming and expensive. Other people start making changes on application level. But That's not really good practice now is it ? MongoDB wanted to provide a simple way make scaling-out (adding more storage, more servers) easier. Data gets split up accross multiple servers. It will balance and load across your cluster. The database needs to figure out how to deal with large amounts of data and clustering, not the developer.

What about RDB

Many people have said that MongoDB is the new MySQL. I do not agree, it is not the new MySQL. Depending on your needs sometimes it still is better to use RDB. Transactions are currently not supported in MongoDB. Also MySQL provides Master-Master setups, where MongoDB only provides Master-slave. However when a master dies, a slave is automatically promoted to master. Also integrity when writing data is not assured. The reason why writing can be fast on MongoDB is because the server does not confirm writes. So if your server is gone, your client (when writing) will not complain.

MongoDB vs RDB

Allthough MongoDB is a document-oriented database, BJ Clark stated that MongoDB is so much more than just a basic key-value storage. It is close to MySQL, but they don't want to be MySQL. MongoDB is ideal for CRUD applications and applications that relly on select on key. MongoDB does not try to be MySQL, it just tries to be better a specific thing within the database products. MySQL has tons of features, datatypes and options. MongoDB hasn't, because you simply don't use these features when building (but not limited to) websites or CRUD applications, you just need flexibility. MongoDB is better when you need:
  • Massive writing
  • Big data
  • Fast key-value access
  • Flexible Datatypes
  • Maintainibility
RDB is better when you need:
  • Data Integrity
  • Data Independence
  • Ad hoc queries
  • Complex relationships

Conclusion

MongoDB is a magnificent tool and database, it doesn't replace MySQL, but in a lot of situations MongoDB is just the better choice where people now use MySQL or another RDB.