The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MongoDB::GridFS - A file storage utility

SYNOPSIS

    use MongoDB::GridFS;

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

SEE ALSO

Core documentation on GridFS: http://dochub.mongodb.org/core/gridfs.

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?, $options?)

    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.

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>