MongoDB (eight) Mongodb--gridfs storage

Source: Internet
Author: User
Tags unique id


MongoDB documents are stored in Bson format and support binary data types when we store binary format data directly into MongoDB's documentation. However, when files are too large, such as samples and videos, each document is limited in length, so MongoDB provides a canonical--gridfs for handling large files.

Gridfs Implementation Principle

In the Gridfs database, by default, Fs.chunks and Fs.files are used to store the file, where the Fs.files collection holds the file's information, fs.chunks the data that holds the file, One of the records in a Fs.files collection is as follows: A file information is shown below.

[SQL]View Plaincopy print?
  1. <pre name="code" class="javascript" >{
  2. "_id": ObjectId ("4f4608844f9b855c6c35e298"),//unique ID, can be a user-defined type
  3. "FileName": "CPU.txt",//File name
  4. "Length": 778,//File length
  5. "ChunkSize": 262144, size of//chunk
  6. "Uploaddate": Isodate ("2012-02-23t09:36:04.593z"),//upload time
  7. "MD5": "E2C789B036CFB3B848AE39A24E795CA6",//MD5 value of the file
  8. "ContentType": MIME type of "Text/plain"//File
  9. "META": NULL//file other information, default is no "meta" this key, the user can define themselves as any Bson object
  10. }



Corresponding to the chunk in fs.chunks (Chinese meaning data block), as follows:

[JavaScript]View Plaincopy print?
    1. {&NBSP;&NBSP;&NBSP;
    2. "_id"  :  ObjectId ( "4f4608844f9b855c6c35e299"),     // Chunk id  
    3. "files_id"  :  ObjectId ( "4f4608844f9b855c6c35e298"),   //file ID, Corresponds to an object in the Fs.files equivalent to the foreign key    of the Fs.files collection;
    4. "n"  :  0,     //file chunk block, if the file is larger than chunksize, it will be divided into multiple chunk blocks    
    5. "QGV ...")      //file binary data, here omit the specific content    
    6. }  


The default size is 256k, so the file into the GRIDFS process, if the file is larger than chunksize, then the file is divided into multiple chunk, then the chunk saved in Fs.chunks, and finally the file information into Fs.files.

When reading the file, according to the conditions of the query, find a suitable record in the Fs.files, get the value of "_id", and then find all files_id _id chunk according to this value to Fs.funks, and sort by "n", and then read the chunk in sequence. The contents of the data object and revert to the original file.


Note:

1, Gridfs does not automatically process MD5 the same file, for MD5 the same file, if you want to have only one store in Gridfs, to user processing, the calculation of MD5 value is done by the client.

2, because Gridfs in the process of uploading files is the first to save the file data to Fs.chunks, and finally save the file information to Fs.files, so if the upload file process failure, there may be garbage data in the fs.chunks, these garbage data, can be cleaned up regularly.

MongoDB (eight) Mongodb--gridfs storage

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.