[GridFS of MongoDB]

GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16 MB.

GridFS is used to store and restore files (such as pictures, audio, video, etc.) that exceed 16M (BSON file limit).

GridFS is also a form of file storage, but in a MonoDB collection.

GridFS can better store files larger than 16M.

GridFS will divide large file objects into multiple small chunks (file fragments), generally 256k/piece, and each chunk will be stored in the chunks collection as a document of MongoDB.

GridFS uses two collections to store a file: fs.files and fs.chunks.

The actual content of each file is stored in chunks (binary data), and the meta data related to the file (filename, content_type, and user-defined attributes) will be stored in the files collection.

 

 

Instead of storing a file in a single document, GridFS divides the file into parts, or chunks , and stores each chunk as a separate document. By default, GridFS uses a chunk size of 255 kB; that is, GridFS divides a file into chunks of 255 kB with the exception of the last chunk. The last chunk is only as large as necessary. Similarly, files that are no larger than the chunk size only have a final chunk, using only as much space as needed plus some additional metadata.

 

GridFS uses two collections to store files. One collection stores the file chunks, and the other stores file metadata. The section GridFS Collections describes each collection in detail.

 

When you query GridFS for a file, the driver will reassemble the chunks as needed. You can perform range queries on files stored through GridFS. You can also access information from arbitrary sections of files, such as to “skip” to the middle of a video or audio file.

 

GridFS is useful not only for storing files that exceed 16 MB but also for storing any files for which you want access without having to load the entire file into memory. See also When to Use GridFS.

 

Changed in version 2.4.10: The default chunk size changed from 256 kB to 255 kB.

 

 

When to Use GridFS

In MongoDB, use GridFS for storing files larger than 16 MB.

In some situations, storing large files may be more efficient in a MongoDB database than on a system-level filesystem.

If your filesystem limits the number of files in a directory, you can use GridFS to store as many files as needed.

When you want to access information from portions of large files without having to load whole files into memory, you can use GridFS to recall sections of files without reading the entire file into memory.

When you want to keep your files and metadata automatically synced and deployed across a number of systems and facilities, you can use GridFS. When using geographically distributed replica sets, MongoDB can distribute files and their metadata automatically to a number of mongod instances and facilities.

Do not use GridFS if you need to update the content of the entire file atomically. As an alternative you can store multiple versions of each file and specify the current version of the file in the metadata. You can update the metadata field that indicates “latest” status in an atomic update after uploading the new version of the file, and later remove previous versions if needed.

 

Furthermore, if your files are all smaller the 16 MB BSON Document Size limit, consider storing the file manually within a single document instead of using GridFS. You may use the BinData data type to store the binary data. See your drivers documentation for details on using BinData.

 

 

Why use GridFS

Since the size of BSON objects in MongoDB is limited, the GridFS specification provides a transparent mechanism to split a large file into multiple smaller documents. This mechanism allows us to efficiently save large file objects, especially for Those huge files, such as videos, high-definition pictures, etc.

 

 

GridFS uses two tables to store data:

files contain metadata objects

chunks binary chunks containing some other relevant information

In order for multiple GridFS named to a single database, both files and chunks have a prefix, by default, the prefix is ​​fs, so any default GridFS store will include the namespaces fs.files and fs.chunks. Various third-party language drivers have permission to change this prefix, so you can try to set up another GridFS namespace for storing photos, its specific location is: photos.files and photos.chunks.

 

32-bit MongoDB processes are limited to about 2 gb of data. This has come as a surprise to a lot of people who are used to not having to worry about that. The reason for this is that the MongoDB storage engine uses memory-mapped files for performance.

 

By not supporting more than 2gb on 32-bit, we’ve been able to keep our code much simpler and cleaner. This greatly reduces the number of bugs, and reduces the time that we need to release a 1.0 product. The world is moving toward all 64-bit very quickly. Right now there aren’t too many people for whom 64-bit is a problem, and in the long term, we think this will be a non-issue.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326466168&siteId=291194637