The database supports native storage of binary data within BSON objects. However, BSON objects in MongoDB are limited in size (4MB older versions, 16MB in v1.7/1.8, higher limits in the future). The GridFS spec provides a mechanism for transparently dividing a large file among multiple documents. This allows us to efficiently store large objects, and in the case of especially large files, such as videos, permits range operations (e.g., fetching only the first N bytes of a file).
這是官網的一段GridFS的介紹. GridFS是提供了把一個大檔案切分成多個document來儲存的. 向GridFS中插入一個檔案, 預設使用fs.files和fs.chunks兩個collection來儲存此檔案的資訊的, 其中fs.files存放了檔案的資訊, fs.chunks存放了檔案的資料.
- > db.fs.files.findOne()
- {
- "_id" : ObjectId("4fbfae0ad417a4f2bc5bc6d1"),
- "filename" : "./Makefile", //檔案名稱
- "chunkSize" : 262144, //chunk的大小, 是固定的, 預設為256*1024
- "uploadDate" : ISODate("2012-05-25T16:06:34.794Z"), //上傳日期
- "md5" : "f9eae9d5987644a537862ca3707ff59d", //檔案的md5值
- "length" : 130 //檔案的長度
- }
-
- > db.fs.chunks.findOne()
- {
- "_id" : ObjectId("4fbfae0a4d460742f1aa76dc"),
- "files_id" : ObjectId("4fbfae0ad417a4f2bc5bc6d1"), //對應fs.files中的_id
- "n" : 0, //檔案的第幾個chunk,這裡要注意 如果檔案大於fs.files中的chunkSize則進行分塊, 從0計數
- "data" : BinData(0,"QWxsOgoJZysrIG1haW4uY3BwIC1ML3Vzci9sb2NhbC9saWIvIC1JL3Vzci9sb2NhbC9pbmNsdWRlIC1sbW9uZ29jbGllbnQg
- LWxib29zdF90aHJlYWQgLWxib29zdF9maWxlc3lzdGVtIC1sYm9vc3RfcHJvZ3JhbV9vcHRpb25zCg==") //檔案的二進位流
- }
下面看具體測試代碼:
- int main(int argc, const char** argv)
- {
- DBClientConnection pConn;
- pConn.connect("10.15.107.154:20000");
-
-
- GridFS* pGridFs = new GridFS(pConn, "TestGF");
-
- #if 1
- ///< 隱藏檔
- pGridFs->storeFile("./Makefile");
- #endif
-
-
- #if 0
- ///< 遍曆檔案
- auto_ptr<DBClientCursor> cursor = pGridFs->list();
- if (cursor->more()) {
- BSONObj obj = cursor->next();
- cout<<obj.toString().data()<<endl;;
- }
- #endif
-
-
- #if 0
- ///< 讀取檔案
- GridFile file = pGridFs->findFile("./Makefile");
- file.write("./hello");
- #endif
-
-
- #if 0
- ///< 刪除檔案
- pGridFs->removeFile("./main.cpp");
- #endif
-
-
- return 0;
- }