GridFS是MongoDB为存取大型文档(超过4mb)准备的。首先是关于GridFS原理性的介绍。
一、GridFS原理
This filesystem within MongoDB was designed for … well, holding files, especially files over 4MB … why 4MB?
Well BSON objects are limited to 4MB in size (BSON is the format that MongoDB uses to store it’s database information) so GridFS helps store files across multiple chunks.
As Kristina Chodorow of 10Gen puts it …
GridFS breaks large files into manageable chunks. It saves the chunks to one collection (fs.chunks) and then metadata about the file to another collection (fs.files). When you query for the file, GridFS queries the chunks collection and returns the file one piece at a time.
Why would you want to break large files in to “chunks”? A lot of of comes down to efficient memory & disk usage.
Say you want to store larger files (maybe a 2GB video) when you preform a query on that file all 2GB needs to be loaded into memo