0

I have read a number of documents till now on MongoDB and am interested in creating a GridFS collection to store files. I already have created the PHP script which can deal with files stored in MySQL (blob datatype) and it is working. However I now need to work with GridFS. PHP Driver documents confused me.

NOTE: The point that confuses me is - they do not tell how to create the 'chunks' collection. There are no guidelines. If it is so then how can I expect the driver to be able to read the files from the 'files' collection automatically?

Can someone point me to a page where it has been explained how to create one? Or may be help me understand the 'expectations' of language drivers for GridFS collection(s?).

Regards Vaibhav

Vaibhav Kaushal
  • 340
  • 1
  • 6
  • 16
  • 1
    There's no 'create a collection' operation in MongoDB. You can insert documents into non-existing collections, that creates the collection. Most drivers automatically do that, and they also create the relevant indexes. What have you tried? Do you have any code? – mnemosyn Sep 11 '13 at 09:32
  • Yes, I know that. But the specifications for the collections was what I wanted. – Vaibhav Kaushal Sep 11 '13 at 12:58

1 Answers1

2

Looking through the PHP documentation I found the following manual on how to store data in a GridFS Collection: PHP GridFS Manual. I don't have any specific knowledge from PHP but in Java I do the following:

GridFS gridFS = new GridFS(<DB_NAME>, <GRID_FS_COLLECTION>);
GridFSInputFile inputFile = gridFS.createFile(<BINARY_DATA>);
inputFile.put("documentName", "nameOfTheFile");
inputFile.put("extension", "txt");
inputFile.save();

Looking at the PHP Manual it would be something similar to this in PHP:

<?php
   $grid = $db->getGridFS();
   $grid->storeBytes($bytes, array("documentName" => "nameOfTheFile", "extension" => "txt"));
?>

The examples in the manual also show a way to store a file directly without getting a string of bytes first.

SterAllures
  • 133
  • 4
  • 13
  • Thanks SterAllures. I do not work on Java. And that is where PHP's methods are confusing me. I think I will have to go through the 'trial and error method'. – Vaibhav Kaushal Sep 11 '13 at 13:17