About Jamie on Software

Jamie on Software is the online journal of web developer and writer Jamie Rumbelow.

Jamie likes books, guitars, programming, open source and food. He writes about these things too. This is where he puts the things he writes.

My Books

Tags
Tweets
Feeds
We Love
Powered by Squarespace

Entries in nosql (1)

Friday
Nov122010

A quick MongoDB primer

The almost exponential growth of the web industry has brought a range of new and exciting technologies for developers to use. One of these is the simply fantastic NoSQL database, MongoDB. Here's a quick primer to those interested in using it in your projects.

Lay the foundations

Get to grips with NoSQL and the NoSQL movement; forget about relational databases like MySQL and start to think of your application in terms of documents instead. Start thinking about how Mongo could fit into your application. It's similar to designing a relational database schema, but instead of worrying about adding in new columns or making links in tables, you just decide what collections you'll be using and that's it.

Get in the trenches

Download MongoDB and get reading the Starter Tutorial. Then, the best thing to do is get started writing an application! Just start getting a flavour of what Mongo's about. One of the best and most useful things about working with your data directly in object-serialisation is that you can just add new columns/keys as you please. No more bloody schemas. 

If you're using PHP, there's already a great PECL extension available, and Alex Bilbie has written a wrapper around it for CodeIgniter. For Rubyers, Mongoid is gorgeous, incredibly easy to use, and even has a Railscast devoted to it. For those more inclined to whitespace significance, you'll almost certainly run into PyMongo, and would be very wise to study and learn this lower-level interface. When you want something higher-level, check out MongoEngine, which is a great ORM that fits in well with Django, but can be used elsewhere.

Going live

Installing and deploying MongoDB databases is very very very easy. If you'd rather not mess about with configuring Mongo - despite it being an incredibly simple task - you can use the frankly fantastic hosted solution, MongoHQ, who will host your databases in a scalable, quick and efficient cloud environment. If you'd rather host yourself, I'd recommend you check out a great post over at the Agile Testing blog that talks in detail about setting up a good Mongo environment.

There's no schema to copy over or run. All you have to do is create the database you'd like to use and you're done. No SQL files or messy schemas. Mongo creates the collections and documents on the fly. It's all dynamic.

A few gotchas

There are a couple of gotchas and things to watch out for. One of the biggest things that gets a lot of people is that there aren't any autoincrement IDs. Instead, MongoDB uses a random hash string, automatically created for every document.

If you want to retain using old autoincrement IDs, you have to go about it in a slightly different way. Check out this great tutorial from Chris Shiflett on autoincrement IDs in MongoDB. Otherwise, you'll have to use the MongoIDs. I'll also mention the fact that in the PHP extension you can't just look it up as a string - it's a serialised MongoID instance.

In fact, Mongo has a number of particular serialised classes for a number of different purposes, including MongoRegex, which can be used for querying the database and, more unusually, storing regular expression patterns, and MongoDate, for DateTimes.

Join the Mongo Revolution

MongoDB is bloody lovely. It's an exciting time to be getting into NoSQL web development, and you'll find that MongoDB is the absolute best NoSQL database that provides a great mix between familiar relational syntax and the power of map/reduce, document searching, indexing and sharding.