MongoDB's learning Note (iv)--MONGODB in Gridfs

Source: Internet
Author: User
Tags mongodb

Gridfs is a file specification for storing large files in MongoDB:
The MongoDB database holds binary objects in support of the Bson format , but the Bson objects in MongoDB cannot exceed 4MB in maximum.
The GRIDFS specification provides a transparent mechanism for splitting a large file into multiple smaller documents. To achieve this, the specification specifies a standard for chunking files. Each file holds a metadata object in the file collection object, and one or more block objects can be grouped together in a block collection.
Gridfs File Storage principle:
The driver will first create two collections in the current database: Fs.files and Fs.chunks, which record the file name, file creation time, file type and other basic information, the latter block stores the binary data of the files (and supports the encryption of these binary data), chunking means to divide the file by the specified size, and then into multiple documents, How does "Fs.files" know where the file is?
In "Fs.chunks" there is a "files_id" key corresponding to "fs.files" of "_id", "Fs.chunks" also has a key (int) "n", it indicates the order of these blocks, the two sets of "FS" can also be customized by the parameters.

A record in a Fs.files collection is as follows: The information for a file is as follows.

The chunk in the corresponding fs.chunks are as follows:

The default chunk size is 256K:
Write:
If the file is larger than chunksize, divide the file into multiple chunk, save the chunk to fs.chunks, and then deposit the file information in Fs.files.
Read:
According to the conditions of the query, find a suitable record in fs.files, get the value of "_id", and then according to this value to Fs.chunks to find all "files_id" to "_id" Chunk, and press "n" to sort, finally read chunk "data" Restores the contents of the object to the original file.

MONGO comes with an implementation mongofiles, the basic operation is as follows:
1. List all Files:

2. Upload a file:

3. To download a file:

4. To find a file:

Parameter description:
-d Specifies the database, the default is FS
-u-p User name, password
-h Specifies the host
-port Specifying host Ports
-c Specifies the collection, the default is FS
-t specifies the MIME type of the file, which is ignored by default
-L Specify Path

MongoDB File access operation:
Http://www.cnblogs.com/lipan/archive/2011/03/21/1989409.html

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.