對(duì)于MongoDB collection下的文檔而言,在文件大于16MB時(shí),我們應(yīng)該使用GridFS。
在一些解決方案中,使用MongoDB的GridFS存儲(chǔ)大文件,比使用系統(tǒng)級(jí)別的文件系統(tǒng)更便利。
.當(dāng)文件系統(tǒng)的目錄對(duì)文件數(shù)量有限制時(shí),你應(yīng)該使用GridFS來(lái)存儲(chǔ)需要的盡可能多的文件;
.當(dāng)你想讓你的文件和元數(shù)據(jù)在多個(gè)系統(tǒng)和設(shè)施之間自動(dòng)同步和部署時(shí)。使用地理上分布的副本集(geographically distributed replica sets),MongoDB可以自動(dòng)地分發(fā)文件和元數(shù)據(jù)到多個(gè)mongo實(shí)例和設(shè)施上。
.當(dāng)你想訪問(wèn)大文件的部分信息,而又不想把整個(gè)文件加載到內(nèi)存時(shí),你可以使用GridFS調(diào)回部分文件,而不用把整個(gè)文件讀入到內(nèi)存。
如果你需要自動(dòng)的更新文件的整個(gè)內(nèi)容,不要使用GridFS.作為替代,你可以存儲(chǔ)文件的多個(gè)版本,并且在元數(shù)據(jù)中指定文件的當(dāng)前版本。在上傳新版本的文件后,你可以以原子更新來(lái)更新表示為"latest"的元數(shù)據(jù)字段,如果需要的話,你可以刪除以前的版本。
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 閱讀(508)
評(píng)論(0) 編輯 收藏 所屬分類:
nosql