Dear Web Developer: MongoDB Rubs Your Feet

John Cusack as Lane Meyer in Better Off Dead (1985)

I graduated from high school in 1986.  If you don’t remember 1986 because you were young, unborn, or in a coma, then you could see Hot Tub Time Machine for an exaggerated version of the time complete with hair and clothing styles and jokes about Michael Jackson and meanie ski patrol guys.

The funny thing about Hot Tub Time Machine, I thought, was that John Cusack appeared in a 1985 movie called Better Off Dead that poked fun at skiing movies and ski-bullies and having your girlfriend stolen by a ski-guy but she’s not worth your time anyway (she went with the ski-bully because she thought he was cooler, and could ski faster anyway) and you spend your time drawing cartoons and don’t even notice that the French exchange student who moved in across the street is both hot and hot for your Lane Meyer.

Mia Sara in Ferris Bueller’s Day Off
(and some other guys)

Lane Meyer is no Lloyd Dobler.

Lloyd Dobler is no Ferris Bueller.

If you had to click that link to find out who Ferris Bueller is then you’re not a 40-something geek who was thrilled to see that the kid who got a computer instead of a car was able to meet and hypnotize a cool, beautiful girl into being his girlfriend.

There is no shame in dating a hot Junior unless you’re an 18-year-old Senior in which case you are a pedophile.

Anyway, I remember going to high school.  I am so thrilled when I read about what members of my class have been doing.  Other than me, those that write appear to be well adjusted and basically happy, with families and serious careers.  A number of them are computer guys.

I was reading Mitch’s blog this morning [http://www.mitchitized.com/] and was reminded that he contributes to MongoDB [http://www.mongodb.org/] which is a document-oriented database that stores and transacts in JSON objects.

JSON is JavaScript Object Notation.

JavaScript is the primary way websites manipulate what you’re seeing.

When you’re sitting on the Twitter or Facebook websites and something pops up, the information was probably received thanks to a call in the background to a web service that returned a JSON object which made it VERY easy for the JavaScript running on the page to format the information for your eyes.

Most websites on the Internet are probably created using a relational database like MySQL or Microsoft SQL.

A relational database is one that is conceptually made up of Tables of data, each Table stores Rows of information organized into Columns.

Table:
People

Columns:
Id, Name, Email

Rows:
1,  Joe,  ireadjoesblog@winett.com
2, Jennifer, jenniferanistongetsfreeemailfromjoe@winett.com

If you want to keep a list of the Pets a person has, then you add another table.

Table:
Pets

Columns:
Id, peopleId, Name, Species

Rows:
165, 1, Sammy, Dog
176, 1, George, Fish
765, 2, Sampson, Tiger

If you’re building a web page to display a list of People and their Pets, then you have to have the server get this data together for you before you spit the page out to the user.

SELECT People.Name as ‘Person’, Pets.Name as ‘Pet’, Species FROM People JOIN Pets ON People.Id = Pets.peopleId

And you get back something like this:
Joe, Sammy, Dog
Joe, George, Fish
Jennifer, Sampson, Tiger


But you’re not going to spit out the list raw, right?  You’re going to organize it — you’re not going to repeat Joe.  You’re going to put up a new section for every new person, then list their pets underneath.  So your code has to look for when the person changes.

OK, fine.

So what if you want to create a dynamic website (sort of like Facebook) where there’s a page that lists all of your friends’ pets and it automatically updates if someone adds a pet.

To do this you use AJAX (which uses an asynchronous call to a web server behind the scenes to get some data).  Your JavaScript program checks the web server for updates every so many seconds.

Your web server could return formatted HTML to be inserted into the page, but then you have two sources for formatted content and that can be a pain unless you have some underlying library of formatting, whatever…

The best choice is to return JSON — a formatted string that can be loaded directly into JavaScript and used.

So, the page should return something like this:

{
   Updates: [
   { Person: ‘Jane’, Pet: ‘Wiggles’, Species: ‘Chimpanzee’ },
   { Person: ‘Jennifer’, Pet: ‘Booker’, Species: ‘Pony’ }
  ]
}

Lots of steps.

MongoDB transacts JSON with you and deals with everything as though they’re complete documents with an arbitrary number elements.

With a relational SQL database, when you want to add a Pet to a Person, you first have to look up the Id of the Person you want to add the pet to, then insert the Pet into a separate table.

With MongoDB, you just push another Pet onto the Person’s array of Pets.  It’s like using JavaScript itself.

You can try it out online using an interactive shell with a little tutorial!

It’s VERY EASY — CLICK HERE —>   http://try.mongodb.org/

Oh, and I also read Mitch’s post, “Goodbye, auto increment” :: Mitch is right: artificial numeric keys really don’t make any sense when you have a source for a natural unique key.  I think it’s a habit that started back when space was expensive and the idea of spreading a long, compound-textual key through the related tables rubbed optimizing nerds the wrong way.  It seemed to make more sense to get a short, 4-byte (HAHA!!) integer for the head table and use it for the relationships.  It also seemed to make more sense that the integer comparisons on the btrees would be faster than text comparisons.  WHAT MITCH POINTS OUT is that when databases get big (and websites like Facebook have BIG databases) then you can’t put everything on one SQL server, so you end up having to split the database into shards and it’s a pain if the key you used originally really doesn’t have anything to do with the data.

Thanks Mitch!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.