對于MongoDB collection下的文檔而言,在文件大于16MB時,我們應該使用GridFS。
在一些解決方案中,使用MongoDB的GridFS存儲大文件,比使用系統級別的文件系統更便利。
.當文件系統的目錄對文件數量有限制時,你應該使用GridFS來存儲需要的盡可能多的文件;
.當你想讓你的文件和元數據在多個系統和設施之間自動同步和部署時。使用地理上分布的副本集(geographically distributed replica sets),MongoDB可以自動地分發文件和元數據到多個mongo實例和設施上。
.當你想訪問大文件的部分信息,而又不想把整個文件加載到內存時,你可以使用GridFS調回部分文件,而不用把整個文件讀入到內存。
如果你需要自動的更新文件的整個內容,不要使用GridFS.作為替代,你可以存儲文件的多個版本,并且在元數據中指定文件的當前版本。在上傳新版本的文件后,你可以以原子更新來更新表示為"latest"的元數據字段,如果需要的話,你可以刪除以前的版本。
For documents in a MongoDB collection, you should always 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 keep your files and metadata automatically synced and deployed across a number of systems and facilities. When usinggeographically distributed replica sets MongoDB can distribute files and their metadata automatically to a number of mongod instances and facilities.
- 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.
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. You may use the BinData data type to store the binary data. See your drivers documentation for details on using BinData.
For more information on GridFS, see GridFS.
地址:http://docs.mongodb.org/manual/faq/developers/#faq-developers-when-to-use-gridfs
posted on 2013-04-09 18:41
zhangxl 閱讀(510)
評論(0) 編輯 收藏 所屬分類:
nosql