< Browse > Home / Uncategorized / Blog article: document databases (MongoDB + PHP tutorial)

| Mobile | RSS

document databases (MongoDB + PHP tutorial)

March 27th, 2010 | 2 Comments | Posted in Uncategorized

The last few months I’ve heard some noise on document-oriented database systems. At first I dismissed it as the next hype in web development, but last night I’ve cave in. Partly because I’m at yet another conference, Scottish Ruby Conference. This community is really focused on the early adoption of new technologies so they may be the tipping point for this hype.

First, lets see what a document-oriented database is.
You know about scheme-oriented databases like MySQL, right? Before you start you’re project, you start thinking what data you want to save. Once you know that, you have to start breaking that up in databases, tables, strongly typed fields and hope you don’t have to leave that path too often during the project…

With document-oriented databases this is not the case. You just create a database and start developing. Every time you need to save some data, you just create a document, add the fields you need (for a shopping card you’d probably save a user_id, date, price, all the items, …) and save it to the database.

From Wikipedia:

As opposed to relational databases, document-based databases do not store data in tables with uniform sized fields for each record. Instead, each record is stored as a document that has certain characteristics. Any number of fields of any length can be added to a document. Fields can also contain multiple pieces of data.

Lets get physical!
I assume you are running on osx 10.6 or higher and have some notion of php

Got 10 minutes?
I’ve chosen for MongoDb after some (quick) looking at benchmarks and comparison charts and it seems Mongo DB is slightly faster at execution. But there are others out there;

So, lets install MongoDB. I’ve chosen for the painless route and installed it using Homebrew. (If you don’t have Homebrew installed, check out the github page)

brew install mongodb

Next, we need to have the mongodb php extension. You could compile the .so file youreself, but again, but again, I chose for the easy solution. Grab the right .so file and place it in your php extension dir. Then add this line to your php.ini file;


Restart apache and you’re ready to go!

Create a folder where you want your databases to reside and fire up mongodb;

mkdir -p ~/data/mongodb
mongod --dbpath ~/data/mongodb/

Go ahead and create a plain php file to play with.

First, we need to establish a connection with our MongoDB server;

$conn = new Mongo();

This will connect to the server on your localhost running on port 27017. You can add a parameter to specify the server location (eg: new Mongo(“mymongoserver:65432″);).

Then select a database like this;

$db = $conn->dbname;

and select a collection;

$albums = $db->albums;

That’s it! Now you can start manipulation the database.

Let’s create a document;

$album = array(
   "title" => "The Fame Monster",
   "artist" => "Lady Gaga"

Now we need to add some tracks. In scheme-oriented database system you would create another table with tracks, and link them to the album with an album_id. Not so document-oriented database systems;

$album["tracks"] = array(
    1 => "Just Dance",
    2 => "LoveGame"

Done! The tracks are saved in the album document itself, not separate documents.
Now we can insert it into the database;


You can read out the documents with the find function;

foreach($albums->find() as $album) {
    foreach($album as $k => $v) {
        echo $k . ": " . $v . "<br />";
    echo "<br />";

And there we have a list of all ablums on the screen.

I just wanted to show you how easy it was, if you want to find out more, here are some links;

If you start playing with is, please do let me know what your think of it.

Leave a Reply 22569 views, 7 so far today |
Follow Discussion

2 Responses to “document databases (MongoDB + PHP tutorial)”

  1. Waldo Says:

    I read a lot of interesting articles here. Probably you spend a lot of time
    writing, i know how to save you a lot of work, there
    is an online tool that creates readable, google friendly articles
    in seconds, just search in google – laranitas free content source

  2. AdultWork Adult Sex Says:

    Very shortly this web site will be famous among all blogging people, due too it’s nice

Leave a Reply