Introduction to MongoDB

Today was the first day of the TEK conference tutorials.  These are generally about 3 hours in length and dive into a given topic a little deeper than what you would typically get in a session. The biggest problem is that you can usually only go to one.  Theater hopping, in this case, means that you end up getting less entertained.

There were 3 topics that were given.

  1. Building a Zend Framework application
  2. Converting Your MySQL App to NoSQL with MongoDB
  3. Bad Guy For a Day: A Websecurity hands-on tutorial

While I had previously said I was going to go to the web security one I somehow found myself in the MongoDB tutorial with Kristina Chodorow.  I'm actually kind of glad that I did.  Kristina went through a LOT of information very quickly and gave me a lot to think about and research in the coming days, weeks and years.  It took only about 37 minutes for her to convince me I needed to look at MongoDB seriously.

If you are new to MongoDB, the easiest way to define it is an associative array on a ginormous amount of steroids.  That is a very, very dumbed down definition but that's how you can start.  The feature set contains Document-oriented storage, Replication & High Availability, Auto-Sharding, Querying, Fast In-Place Updates, Map/Reduce and GridFS.  I am currently most interested in the document oriented storage and map/reduce.

The reason I am interested in Document-oriented storage is because as you scale with the amount of data, having perfect relations will become hard and harder.

The reason I am interested in Map/Reduce is because everyone else is talking about it.  I don't do a lot of large data set processing, but if I HAD to I want to know what I need to do.

So, what I could do is go through all of the things that Kristina talked about.  However, then I would miss the talk that Matthew and Lorna are giving.  So what I'm going to do is give you the basics.

First of all, go to the MongoDB website and download a copy.  I am running on Windows right now so I downloaded the Windows version.  I unzipped it on to Desktop, opened up a command shell and started up mongod.exe from the command line.  But prior to starting I needed to create a /data/db directory so Mongo had a place to store its data.  Then I started it.

Starting Mongo

Then what I did was go to the Mongo site and download the PHP driver.  I'm using Zend Server for PHP 5.2 (because I was waiting for the 5.3 download and conference Wifi connections are never good).  So I just copied the NTS Mongo PHP extension and pasted it into the #zendserver/lib/php_ext directory.  From there I opened up the Zend Server extension page and enabled it, restarted and I was ready to go.

Enabling Mongo

At this point, I assume, Mongo DB was running.  So I created a test script and created a new Mongo connection.

$drv = new Mongo();

I ran the script and there were no errors so I was good.  So, the first thing I did was went through the Quickstart guide. The first thing I did was populate the DB with a little bit of data.


So far so good.  The next thing is to try and get that data into PHP.  For that I wrote a very, very simple script.

$drv = new Mongo();
$db = $drv->mydb;

foreach ($db->things->find() as $doc) {

Make sure that you compare the structure between the data I entered and the PHP code I wrote.  My $db is "mydb", compared to "use mydb" in the console.  My table (or collection as it is actually called) is "things". To iterate over the data I need to select my database, select my collection and find() the items on there.  When I run this code I get

array(2) { ["_id"]=> object(MongoId)#6 (0) { } ["name"]=> string(5) "mongo" } array(2) { ["_id"]=> object(MongoId)#7 (0) { } ["x"]=> float(3) }

That's about where I will leave it today.  I am completely new to MongoDB but I was able to get it up and running in about 10 minutes.

There is a lot more to do, but this is enough to get you started.

3 Thoughts to “Introduction to MongoDB”

  1. I’ve start a tutorial series about PHP and MongoDB called “PHP & MongoDB Sitting in a Tree”. Chec out the first tutorial @

    On a side note, the “human prover” doesn’t work in IE.

Leave a Reply

Your email address will not be published.