MongoDB Finishing Note のgridfs

Source: Internet
Author: User
Tags findone

Gridfs is a file specification that stores large files in a MongoDB database. All officially supported drivers implement the GRIDFS specification.

Gridfs is a built-in feature in MongoDB that can be used to store a large number of small files.

Website study:

Http://www.mongodb.org/display/DOCS/GridFS

Http://www.mongodb.org/display/DOCS/GridFS+Specification

1, why to use Gridfs

Because the Bson object size is limited in MongoDB, the GRIDFS specification provides a transparent mechanism for splitting a large file into smaller documents, allowing us to effectively save large file objects, especially for large files such as video, high-definition images, etc.

2. How to realize mass storage

To achieve this, the specification specifies a standard for chunking files. Each file will hold a metadata object in the file collection object, and one or more chunk block objects can be grouped together in a chunk block collection. In most cases, you don't need to know the specifics of this specification, but you can focus on the parts of the Gridfs API or how to use the Mongofiles tool in each language version of the driver.

3. Language support

Gridfs supports Java, Perl, PHP, Python, Ruby, and other programming languages, and provides a good API interface.

4. Brief introduction

Gridfs uses two tables to store data:

Files contains Metadata objects

Chunks binary blocks that contain some other relevant information

In order for multiple Gridfs to be named a single database, both the file and the block have a prefix, by default, the prefix is FS, so any default Gridfs storage will include the namespace Fs.files and Fs.chunks. The drivers for various third-party languages have permission to change this prefix, so you can try to set up another Gridfs namespace for storing photos, which are in the exact location: Photos.files and Photos.chunks. Let's take a look at the actual example.

5. Command line tools

Mongofiles is a tool to manipulate Gridfs from the command line, for example, we will "testfile" this file into the library, the use of the following:    [Email protected] bin]#. /Mongofiles put testfile    127.0.0.1    added file: {_id:objectid (' 4fc60175c714c5d960fff76a ') , FileName: "Testfile", chunksize:262144,    new Date (1338376565745), MD5: " 8ADDBEB77789AE6B2CB75DEEE30FAF1A ", Length:    + done    !

Let's look at what Gridfs files in the library, add a parameter "list" after "Mongofiles"

[[email protected] bin]#./mongofiles list    127.0.0.1    16

Then we'll go into the library and see if there's anything new.

[[email protected] bin]#./MONGO    1.8.1    connecting to:test    > Show Collections     - fs.chunks    mentioned above- fs.files    system.indexes    system.js    >

We continue to see what's in Fs.files

> db.fs.files.find ()    "_id": ObjectId ("4fc60175c714c5d960fff76a"), "filename": "Testfile", " ChunkSize ": 262144,    " uploaddate ": Isodate (" 2012-05-30t11:16:05.745z ")," MD5 " :    " 8addbeb77789ae6b2cb75deee30faf1a "," Length ": 16}

Field Description:
FileName: the stored file name
Chunksize:chunks the size of the tile
Uploaddate: Storage Time
MD5: MD5 code for this file
Length: File size, unit "bytes"
It appears that some of the underlying meta-data information is stored in the Fs.files
We continue to see what's in Fs.chunks

> db.fs.chunks.find ()    "_id": ObjectId ("4fc60175cf1154905d949336"), "files_id" :    ObjectId ( "4fc60175c714c5d960fff76a"), "n": 0, "Data" :    bindata (0, "sgvyzsbpcybczwlqaw5ncg==")}

One of the more important fields is "n", which represents the ordinal number of the chunks, starting from 0, it appears that some actual content data information is stored in fs.chunks.
Now that we can put this file in, we should have a way to take it out and look at the example below:

[email protected] bin]# rm testfile    RM: Delete general file "Testfile"? Y--Delete the file First    [[email protected] bin]#. /mongofiles get testfile-- Take it out     of the library 127.0.0.1 done    write To:testfile    -- checksum MD5, The results are the same as in the library    8addbeb77789ae6b2cb75deee30faf1a testfile    [[email protected] bin]#

6. Index

true});

In this way, a block can be retrieved using its files_id and n values. Note that Gridfs can still get the first block with FindOne, as follows:

Db.fs.chunks.findOne ({files_id:myfileid, n:0});

7. Application

MongoDB provides a command line tool that Mongofiles can handle Gridfs in the bin directory.
List all Files:
Mongofiles List
Upload a file:
Mongofiles put Xxx.txt
To download a file:
Mongofiles Get Xxx.txt
To find a file:
Mongofiles Search XXX//will find all files that contain "xxx" in the file name
Mongofiles list XXX//will find all files with the file name prefixed with "XXX"

MongoDB Finishing Note のgridfs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.