Mongodb--gridfs

Source: Internet
Author: User
Tags aliases md5 unique id mongo shell

Gridfs is a specification for storing and retrieving files that exceed the Bson document size limit of MB.

Instead of storing the file in a document, the Gridfs file is divided into sections, or blocks, each as a separate document. Using Fridfs with Shards without

Off By default, the grid file system block size is limited to 255 K. The Gridfs file system uses two collections to store files. A collection of stored file blocks,

Other store file meta data.


When you query the Gridfs file system to store files, the driver or client will reassemble the blocks. You can perform a range query on the Gridfs file system

File storage. You can also access information from any part of the file, you can "skip" the video or audio file.

The grid file system is not only useful for storing files that are more than MB, but also for storing any files that you want to access without having to load the entire file inside

stored in.


What is the use of?

In the MongoDB document collection, you should always use Gridfs to store files larger than MB. In some cases, the MongoDB database is more than a system-level file

It may be more efficient for the system to store large files.

(1) If your file system restricts the number of files in a directory, you can use the Gridfs file system to store as many files as you need.

(2) When you want your files and meta data to automatically synchronize and deploy multiple systems and facilities. When using a distributed replica set MongoDB can distribute files and

Metadata automatically generates Mongod instances and facilities.

(3) When you want to access the information from some large files, without having to load all the files into memory, you can use Gridfs to recall some files without

Reads the entire file into memory.

do not use Gridfs if you need to automatically update the contents of the entire file. Instead, you can store each file in multiple versions, each file, and specify the current version of the metadata file.

After uploading a new version of the file, you can update the metadata field and indicate the latest status in the atomic clock. Then delete the previous version.


Implement Gridfs

Use the Gridfs file system to store and retrieve files:

(1) Introduction of drive

(2) mongofiles MONGO shell command line tool. Refer to document Click to open link


Gridfs Collection
Gridfs stores files in two collections:
Block Collection-stores binary data blocks.

Each document collection in the block represents a separate block Gridfs file. The following is a collection of prototype document blocks.

{  "_id": <objectid>,  "files_id": <objectid>,  "n": <num>,  "data": <BINARY>}

The Block collection contains the following fields:
chunks._id unique Objectid block.
CHUNKS.FILES_ID Parent class document transaction ID
The serial number of the CHUNKS.N block. Gridfs File system data block, starting from 0.
The payload of the Chunks.data block is a Bson binary type.
Block uses compound index file collection ID and n


file--stores the metadata for the file.


The collection of files for each document represents a file store in the Gridfs file system. Consider the following file collection for the prototype document:

{  "_id": <objectid>,  "Length": <num>,  "chunkSize": <num>,  "Uploaddate": < Timestamp>,  "MD5": 

The file collection for the document contains some or all of the following fields. The application can create additional arbitrary fields:

FILES._ID The unique ID of this document. ID is the data type selected for the original document. BSON the default type of ObjectId MongoDB document.

The size, in bytes, of the Files.length file.

Files.chunksize the size of each block. The Gridfs file system divides the document into a specified block size. The default size is 255 bytes. Version 2.4.10

This change: The default size changes from K to K blocks.

Files.uploaddate the date that the document was first stored by GRIDFS. The date type for this value.

FILES.MD5 MD5 The command returned by the hash filemd5. This value is a string type.

Files.filename is optional. The name of a readable document.

Files.contenttype is optional. The MIME type of a legitimate document.

Files.aliases is optional. An array of alias strings.

Files.metadata is optional. Want to store any additional information.



Gridfs Index


The Gridfs file system uses a unique, composite index file of block collection IDs and N fields. The ID field contains the ID of the file block of the "parent" document. N

The field contains the sequence number of the block. Grid file system data block, starting from 0.

The Gridfs file system index allows valid retrieval IDs and n values for the file blocks that are used, as shown in the following example:

cursor = Db.fs.chunks.find ({Files_id:myfileid}). Sort ({n:1});

If your driver does not create this index, use the MONGO shell to do the following:

Db.fs.chunks.ensureIndex ({files_id:1, n:1}, {unique:true});


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