Dave Cross: Still Munging Data With Perl: Online event - Mar 17 Learn more

NAME

MongoDB::GridFS - A file storage utility

SYNOPSIS

my $grid = $database->get_gridfs;
my $fh = IO::File->new("myfile", "r");
$grid->insert($fh, {"filename" => "mydbfile"});

ATTRIBUTES

chunk_size

The number of bytes per chunk. Defaults to 1048576.

files

Collection in which file metadata is stored. Each document contains md5 and length fields, plus user-defined metadata (and an _id).

chunks

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).

METHODS

find_one ($criteria?, $fields?)

my $file = $grid->find_one({"filename" => "foo.txt"});

Returns a matching MongoDB::GridFS::File or undef.

remove ($criteria?, $just_one?)

$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.

insert ($fh, $metadata?)

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.

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:

# 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);

drop

@files = $grid->drop;

Removes all files' metadata and contents.

all

@files = $grid->all;

Returns a list of the files in the database.

AUTHOR

Kristina Chodorow <kristina@mongodb.org>