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