31 Jul 2010

MongoDb - my first impressions

I've been toying on new things of late by giving a shot at new frameworks, standards and general development tools. And MongoDb has been one of the new love.

There is this project I'm playing with that has something along the lines of a contact manager. Thing is the app involves way too many database writes. An example is importing a set of contacts (in VCard). Each contact imported performs multiple writes across 4 different tables. In short, importing a collection of 100 contacts (zipped) performs nothing less than 500 writes and could even be as much as 800 depending on the availability of different contact elements (VCard standard) like addr, role, email, org, etc. Profiling (locally on my PC), I got around 20 sec (including script execution time) for an import action of a collection of 144 vcard contacts using MySQL as my DB. This is quite worrysome. I switched the database engine to Mongo and killed the relationships between the 4 tables used for contacts insertion to one. Guess what? I got just around 2 seconds against MySQL's 20 secs. The write speed is just so great!

I've since been converting the whole project to use MongoDb. It's quite challenging though but its been fun. The very big challenge is having to do away with table relationships and joins. The plus however is that been non-relational, you are not constrained by the table's standard columns; meaning that each row in a table can have different columns (or fields). Say I have this in MySQL:

It's interpretation in Mongo will simply go like

1
2
3
4
5
6
7
8
9
10
11
12
13
[
    _id: "*some string of 24 hexidecimal chars*",
    // Sorry, no auto increment ids (there are workarounds though)

    name: "Opeyemi Obembe",
    email: ["ray@ngbot.com", "ray@devedgelabs.com"],
    tel : "08066887840"
]
[
    _id: "***",
    name: "Ernest Ojeh",
    email: ["ernest@ngbot.com"], // Just an email

    pro: 1
]

Soon I hope I will be able to share some quite trouble necked queries and their workarounds

My name is Opeyemi Obembe. I build things for web and mobile. You should follow me on Twitter (@kehers).