Most of us grew up on the concept of LAMP(Linux apache mysql and php) but there are times when that basic setup can become a real pain to maintain and scale. Generally the hardest part to scale out of a LAMP setup is MySQL. When running a large site with lots of data and traffic optimizing/scaling MySQL can be a really complicated undertaking.
That is where NoSQL comes into play. NoSQL is a very board term and some options are better than others. At the moment my choice for a NoSQL database is set firmly with MongoDB. Despite being a relatively new comer to the NoSQL scene MongoDB is quite mature and has awesome documentation.
If you are on a *nix platform you can simply use pecl pecl install mongo
For windows you will need to download the MongoDB binaries and the php extensions
Also don't forget to add the mongo extension to your php.ini extension=mongo.so
Now that was easy wasn't it? That is almost always a good sign when the installation is a breeze
For those of us who only have experience with relational databases such as MySQL, Postgresql, etc understanding a Document Oriented database may be a bit difficult at first. Lets start by defining some of the key differences between relational sql databases and non-relational documented oriented databases Documents do not have a predefined schema like tablesJoins are not natively supported but can be manually coded
Establishing a connection with mongo db is really easy $mongo = new Mongo();
or you can define an address $mongo = new Mongo('127.0.0.1:1234');
Now we must select a database to work with. $db = $mongo->ebooks;//or we can also use$db = $mongo->selectDB('ebooks');
It is important to note that if the database does not already exist one will be created auto-magically.
Instead of using 'tables' like with a relational sql database we use 'Collections'. Simply put these are collections of documents. From a traditional sql developer standpoint these would be comparable to a database table(collection) and an entry within that table(document). However, this is much for flexible than that. Each document can have sub documents etc.
Creating or retrieving a collection: $collection = $db->ebooks;//or like above$collection = $db->selectCollection('ebooks');
Just as above, if the collection does not exist one will be created. The max length of the collection name is 128 chars. Although, that includes the db name and indexes. So, it is generally better to stick with a name that is 80 or less chars in length.
Inserting a document is really easy $authors = $db->authors;$author = array( 'first_name'=>'Thomas', 'last_name' =>'Johnson', 'website'=>'http://www.tomfmason.net');$author_id = $authors->insert($author);
The return value of the insert is the object id. Also, you can pass an additional argument 'safe' which tells mongo to return immediately. This is much faster but you wont know immediately if the insert succeeded or not. That option would be really useful if you were inserting a large number of documents.
Lets say we have the following ebook documents in our ebooks collection. $ebook = array( 'title'=>'An introduction to PHP and Mongodb', 'description'=>"Mongodb is an up and coming document oriented database that supports many awesome features including native PHP extension, an easy to use query interface and it is blazing fast. ", 'author_id'=>$author_id, 'reviews'=>array());$ebooks->insert($ebook);
and $ebook = array( 'title'=>'php and mongo: A simple scalable CMS tutorial', 'description'=>"Some nice description here. ", 'author_id'=>$author_id, 'reviews'=>array());$ebooks->insert($ebook);
We can retrieve the author's ebooks like so $filter = array( 'author_id'=>$author_id);$my_ebooks = $ebooks->find($filter);foreach($my_ebooks as $book) { echo $book['title'] . " "; echo $book['description'] . " ";}
Something that caused me a bit of trouble was querying using an id. Lets say our author_id is a string like 4cffb213726e24640d000000. If you tried the following you would get back an empty result $author_id = "4cffb213726e24640d000000"$filter = array('_id'=>$author_id);$author = $authors->findone($