MongoDB has been around for a while now, with the current version of production-level code at 1.4.3. Full integration to PHP is available via PECL, across platforms, or precompiled binaries. And yes, it’s Open Source.
From the site itself, MongoDB puts itself out there as a database that bridges the gap between key-value stores (which are fast and highly scalable) and traditional RDBMS systems (which provide rich queries and deep functionality), and is geared toward document-type storage. It supports Map/Reduce for proper high-speed iteration through high volumes of data, allows for easy replication and HA (high availability – but you knew that already). Who uses it? Sourceforge, bit.ly, github and the New York Times, to name a few, use MongoDB in production, as does disqus, and shutterfly. It outputs JSON-style data structures.
MongoDB (from “humongous”) is a scalable, high-performance, open source, document-oriented database.
Introduction
Good conceptual tutorial is available here — SQL per se is not used, as the functionality is abstracted:
You may notice, in the examples below, that we never create a database or collection. MongoDB does not require that you do so. As soon as you insert something, MongoDB creates the underlying collection and database. If you query a collection that does not exist, MongoDB treats it as an empty collection.
Switching to a database with the use command won’t immediately create the database – the database is created lazily the first time data is inserted. This means that if you use a database for the first time it won’t show up in the list provided by `show dbs` until data is inserted.
To start the server (if you are running in a non-default environment with an existing /data/db
directory), pass --dbpath=FILEPATH
to mangod
Use show dbs
to show all databases, use DBNAME
to switch to the database, and for collections, show collections
works.
Insert
Instead of INSERT INTO
syntax, you save
into the collection you give a name, eg,
newbobject = { mykey : "this is a value to the key called mykey" };
db.mycollection.save(newobject);
Select
And to read the value back, instead of SELECT FROM
, you find
– and LIMIT
is replaced by a variable passed in limit()
from find()
(eg db.find().limit(3)
)– you can also pass your parameter key/value combination to find (eg db.mycollection.find({mykey:"text"})
:
db.mycollection.find();
There’s a full set of info at the tutorial, but here’s a good quote:
A few things to note :
- We did not predefine the collection. The database creates it automatically on the first insert.
- The documents we store can have any “structure” – in fact in this example, the documents have no common data elements at all. In practice, one usually stores documents of the same structure within collections. However, this flexibility means that schema migration and augmentation are very easy in practice – rarely will you need to write scripts which perform “alter table” type operations.
- Upon being inserted into the database, objects are assigned an object ID (if they do not already have one) in the field _id.
- When you run the above example, your ObjectID values will be different.
MongoDB in PHP
In PHP, the code is simplified even more, using an insert
function: $mycollection->insert( $newobject );
In PHP, the select equivalent to the find
is also (surprise, surprise) the find
function:
$query = array( "i" => 71 );
$cursor = $collection->find( $query );
while( $cursor->hasNext() ) {
var_dump( $cursor->getNext() );
}
If you want to select with a where parameter set, say 15 < i <= 20, the code below would cover the SELECT FROM ... WHERE
query; the dollar signs below are escaped as the dollar sign is used as a delimiter for Mongo; preset it to another character (eg ‘:’ and you’d ":gt" => 15, ":lte" => 20
)
<?php
$query = array( "newkey" => array( "\$gt" => 15, "\$lte" => 20 ) );
$cursor = $coll->find( $query );
while( $cursor->hasNext() ) {
var_dump( $cursor->getNext() );
}
?>
Indexing is also simple via ensureIndex, where the assigned value to the index key determines whether you’re indexing ascending or descending:
$mycollection->ensureIndex( array( "newkey" => 1 ) );
And that’s it – quick and simple! Another time, there’ll be more on MongoGridFS – an extension that facilitates the storing and retrieval of documents in the Mongo database; data storage in the DB with metadata!
Download from the download matrix here for OS X, Linux, Windows and Solaris.
Vork
Check back for a review of Vork, which puts itself forward as a high-performance enterprise framework. “Vork is an open-source PHP framework designed for rapid development of performance-oriented scalable applications.” In the meantime, have a look at the intro to Vork.