Skip to content

Instantly share code, notes, and snippets.

@sharpred
Created May 23, 2012 12:00
Show Gist options
  • Save sharpred/2774847 to your computer and use it in GitHub Desktop.
Save sharpred/2774847 to your computer and use it in GitHub Desktop.
retrieve mongo attachment data, iterate through and retrieve each file
<?php
$mongoDB = new Mongo();
$database = $mongoDB->selectDB("BVS");
$collection = $database->createCollection('fs.files');
//to get the attachments metadata back
$query = array("metadata.formdata.claimid" => "SUS14052012-001");
// $items is a cursor of mongodata
$items = $collection->find($query);
//iterate through the collection and retrieve the named file
$grid = $database->getGridFS();
foreach ($items as $item => $value) {
$fileName =$value["metadata"]["uploaddata"]["filename"];
// md5 represents a unique key for a file so is a good one to use
$md5 = $value["md5"];
$file = $grid->findOne(array("md5" => $md5));
$target = './' . $fileName;
$file->write($target);
}?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment