As a quick followup to the last blog, I was asked to demonstrate how we could include some metadata with a file during or after upload.

This is a great feature since we can retrieve all the information attached with a document during a query. Including metadata (i.e. comments, dates, votes or WHATEVER ELSE YOU LIKE) is just as easy.

We are going to pick up the ball at the point where you’ve got all your MongoDB objects ready:

<?php
$date = new MongoDate();
//Use $set to add metadata to a file
$metaData = array('$set' => array(“comment”=>”MongoDB is awesome”, “date”=>$date));

//Just setting up search criteria
$criteria = array('_id' => $id);

//Update the document with the new info
$db->grid->update($criteria, $metaData);

?>

Did you break a sweat? Probably not.

BTW, I threw in a little extra tidbit there with MongoDate(). It’s a better way of inputting a date into Mongo instead of using PHP date with a bunch of formatting to get it just right.

I’ve been tweeting quite a bit about MongoDB over the past few weeks and its time to blog.

About 2 months ago we decided to install and mess around with MongoDB. We went from messing around to serious adoption about 2 weeks ago when we realized the power of working with it and PHP. It was Mitch Pirtle of www.spacemonkeylabs.com that first pointed us in this direction. Mitch was very enthusiastic about Mongo as he explained the great potential. Yet, it was only until I added an array of data from PHP into Mongo that my eyes started to open up.

Go ahead, open another tab in your browser (KEEPING THIS ONE OPEN) and Google MongoDB if you haven’t already (I made it easy providing the link). You’ll find sufficient documentation to give you the warm and fuzzy that 10gen isn’t messing around. They are in the database game for the long haul.

During our play with MongoDB we realized how true the term NoSQL is. For all you SQL grease monkeys out there it’s liberating. No Joins, No Select, just plain NoSQL.

As an example, let’s start off with putting uploaded files “directly” into MongoDB via GridFS (the storage specification for handling large files). Here is how simple it is:

First, we are going to assume you already have the code in place to handle the upload itself. We first tested this using SWFUpload which provides quite a bit of flexibility if you like to control how your page looks during upload. From a PHP perspective, the uploaded file will be accessible via the predefined variable $_FILES.

Here is what you have to do to get the upload into MongoDB:

<?php

$m = new Mongo(); //connect
$db = $m->selectDB("example"); //select Mongo Database

$grid = $db->getGridFS(); //use GridFS class for handling files

$name = $_FILES['Filedata']['name']; //Optional - capture the name of the uploaded file
$id = $grid->storeUpload('Filedata',$name) //load file into MongoDB

?>

Yep, that was it. One thing that took me a “second” to realize is that you actually pass the literal name of the file_post_name (in the example ‘Filedata’) to MongoDB. It does all the heavy lifting of getting the data from the system and storing it. Also, take note that you get an $id back which is the MongoID that acts as the “primary key”. That makes it easy for you to reference the file right away if you need to.

So that’s a rather quick look and tip for MongoDB. Stay tuned  as we continue to put out our MongoDB tricks and tips for PHP. We’re in it for the long haul too.

Where did January go?

Where did 2009 go?

Our last blog post was in December?

Wow, time is flying by. The good news is we’ve been quite busy here at LightCube. The bad news is we are breaking the rule of blogging regularly (sorry).

Looking back, the past two years has brought its challenges and lessons. But we are now looking forward and gearing up for what’s in store. Speaking of which, where is what we’ve been up to:

Tienda! – Who says the internet doesn’t need another eCommerce tool? If you check with the Joomla community you’ll quickly see there is a massive need. We’ve been working with Dioscouri to bring the first native eCommerce application to Joomla! The really exciting news is that Tienda v0.2.0 (Alpha) is available.

MongoDB – For a SQL Grease Monkey like myself this is a huge redirection. Don’t worry, we’re not abandoning our traditional LAMP stack just yet. It’s not so much about leaving MySQL as much as it is about being liberated to use something else. We’re already working on a few pet projects to use MongoDB for our core ACL and automatic MediaRSS feed generating.

WebEDI – What is EDI you ask? It’s a LONG story. But I can tell you this much,  it has been around before the internet and isn’t going away anytime soon. We are helping to enhance the EDI back office systems of companies that are warming up to web services and XML.

I did say a quick summary didn’t I? Although tomorrow isn’t promised to anyone this year is going to be a busy one. We’ll keep you posted (hopefully in increments shorter than a month).