Vaibhav Kaushal
Vaibhav Kaushal

Reputation: 340

How to create GridFS collection?

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

Upvotes: 0

Views: 2324

Answers (1)

SterAllures
SterAllures

Reputation: 133

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.

Upvotes: 2

Related Questions