MongoDB::GridFS - A file storage utility
use MongoDB::GridFS; my $grid = $database->get_gridfs; my $fh = IO::File->new("myfile", "r"); $grid->insert($fh, {"filename" => "mydbfile"});
Core documentation on GridFS: http://dochub.mongodb.org/core/gridfs.
The number of bytes per chunk. Defaults to 1048576.
Collection in which file metadata is stored. Each document contains md5 and length fields, plus user-defined metadata (and an _id).
Actual content of the files stored. Each chunk contains up to 4Mb of data, as well as a number (its order within the file) and a files_id (the _id of the file in the files collection it belongs to).
my $file = $grid->find_one({"filename" => "foo.txt"});
Returns a matching MongoDB::GridFS::File or undef.
$grid->remove({"filename" => "foo.txt"});
Cleanly removes files from the database. If $just_one is given, only one file matching the criteria will be removed.
$just_one
my $id = $gridfs->insert($fh, {"content-type" => "text/html"});
Reads from a file handle into the database. Saves the file with the given metadata. The file handle must be readable. $options can be {"safe" = true}>, which will do safe inserts and check the MD5 hash calculated by the database against an MD5 hash calculated by the local filesystem. If the two hashes do not match, then the chunks already inserted will be removed and the program will die.
$options
{"safe" =
Because MongoDB::GridFS::insert takes a file handle, it can be used to insert very long strings into the database (as well as files). $fh must be a FileHandle (not just the native file handle type), so you can insert a string with:
MongoDB::GridFS::insert
$fh
# open the string like a file my $basic_fh; open($basic_fh, '<', \$very_long_string); # turn the file handle into a FileHandle my $fh = FileHandle->new; $fh->fdopen($basic_fh, 'r'); $gridfs->insert($fh);
@files = $grid->drop;
Removes all files' metadata and contents.
@files = $grid->all;
Returns a list of the files in the database.
Kristina Chodorow <kristina@mongodb.org>
To install MongoDB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MongoDB
CPAN shell
perl -MCPAN -e shell install MongoDB
For more information on module installation, please visit the detailed CPAN module installation guide.