Five Reasons to use a NoSql Database

Yesterday I had a job interview. It was the first of two parts and it was done over the phone. On my CV I had put my experience with Mongodb which was based around some dashboard code written to monitor clicks on a website. I’ll dig that out and post it when I get chance because it was pretty cool. Anyway I digress … One of the questions I was asked was about Mongo was in what circumstances would I use it over a normal relational database.

In the heat of the interview I failed badly and couldn’t think of any particular reason. After I put down the phone I sat thinking and was able to come up with one reason. I was really not satisfied with only one, so I went away and looked up some others. Here they are, five reasons to use NoSql.

  • NoSql databases are schemaless meaning significant benefits when creating, upgrading and changing structure and data. Tables are known as collections and rows as documents. Documents can be significantly different in the same collections meaning a ‘dangerous’ amount of flexibilty. Makes it super easy to collect data when you don’t have all the data and you don’t need to enforce contraints.

  • Changing the structure of a collection doesnt mean lots of downtime while backups are made and new columns added. It takes a lot of time to add a new column to a table with millions of records.

  • Data is stored as Json and queried with Json meaning clean and semantic questions that make sense and don’t get confusing with horrible joins. Just look at this query on Stackoverflow. Yikes.

  • Speed. High speed delivery of Json with built in entity level caching out of the box in many cases.

  • It’s different. Just bear with me here. I’m a big fan of thinking outside the box and looking at different ways of doing things because more often than not, we hold old established views on things that need revsiting from time to time. Thinking differenty about the fundamentals is healthy. Ask more questions!

Bonus

I was so exited about the feelings this stirred up ! (honestly) that I fired up node and installed Mongodb. Can you guess what this does?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Retrieve
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
if(err) { return console.dir(err); }
var collection = db.collection('test');
var docs = [{mykey:1}, {mykey:2}, {mykey:3}];
// insert
collection.insert(docs, {w:1}, function(err, result) {
// find
collection.findOne({mykey:1}, function(err, item) {
// do something with the item !
});
});
});

That’s quite lovely right?