1. 程式人生 > >【NOSQL】非關係型資料庫MongoDB ( MongoDB檔案存取操作 )

【NOSQL】非關係型資料庫MongoDB ( MongoDB檔案存取操作 )

由於MongoDB的文件結構為BJSON格式(BJSON全稱:Binary JSON),而BJSON格式本身就支援儲存二進位制格式的資料,因此可以把檔案的二進位制格式的資料直接儲存到MongoDB的文件結構中。但是由於一個BJSON的最大長度不能超過4M,所以限制了單個文件中能存入的最大檔案不能超過4M。為了提供對大容量檔案存取的支援,samus驅動提供了“GridFS”方式來支援,“GridFS”方式檔案操作需要引入新的程式集“MongoDB.GridFS.dll”。下面我們分別用兩種方式來實現。

一、在文件物件中存取檔案

  當檔案大小較小的時候,直接存入文件物件實現起來更簡潔。比如大量圖片檔案的存取等,一般圖片檔案都不會超過4M。我們先實現一個上傳圖片存入資料庫,再取出來寫回頁面的例子:

   1. 把圖片存到BJSON中

?
1 2 3 4 5 6 7 8 9 10 /// <summary> /// 把圖片存到BJSON中 /// </summary> public void SaveImgBJSON(byte[] byteImg) { Document doc = new Document(); doc["ID"] = 1; doc["Img"
] = byteImg; mongoCollection.Save(doc); }

   2. 獲取BJSON方式儲存的圖片位元組資料

?
1 2 3 4 5 6 7 8 /// <summary> /// 獲取BJSON方式儲存的圖片位元組資料 /// </summary> public byte[] GetImgBJSON() { Document doc=  mongoCollection.FindOne(new Document { { "ID", 1 } }); return doc["Img"] as Binary; }

  上面兩段程式碼是在對MongoDB相關操作進行BLL封裝類中新增的兩個方法,封裝方式檢視上節內容。下面看看在webform中如何呼叫:

  在介面拖出一個FileUpload控制元件和一個Button控制元件,頁面cs類加如下方法:

?
1 2 3 4 5 6 7 protected void Button1_Click(object sender, EventArgs e) { ImgBLL imgBll = new ImgBLL(); imgBll.DeleteAll(); imgBll.SaveImgBJSON(FileUpload1.FileBytes); Response.BinaryWrite(imgBll.GetImgBJSON()); }

二、用GridFS方式存取檔案

  在實現GridFS方式前我先講講它的原理,為什麼可以存大檔案。驅動首先會在當前資料庫建立兩個集合:"fs.files"和"fs.chunks"集合,前者記錄了檔名,檔案建立時間,檔案型別等基本資訊;後者分塊儲存了檔案的二進位制資料(並支援加密這些二進位制資料)。分塊的意思是把檔案按照指定大小分割,然後存入多個文件中。"fs.files"怎麼知道它對應的檔案二進位制資料在哪些塊呢?那是因為在"fs.chunks"中有個"files_id"鍵,它對應"fs.files"的"_id"。"fs.chunks"還有一個鍵(int型)"n",它表明這些塊的先後順序。這兩個集合名中的"fs"也是可以通過引數自定義的。

  如果你只是想知道怎麼用,可以忽略上面這段話,下面將用法:

   1. GridFS方式的檔案新建,讀取,刪除

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 private string GridFsSave(byte[] byteFile) { string filename = Guid.NewGuid().ToString(); //這裡GridFile建構函式有個過載,bucket引數就是用來替換那個建立集合名中預設的"fs"的。 GridFile gridFile = new GridFile(mongoDatabase); using (GridFileStream gridFileStream = gridFile.Create(filename)) { gridFileStream.Write(byteFile, 0, byteFile.Length); } return filename; } private byte[] GridFsRead(string filename) { GridFile gridFile = new GridFile(mongoDatabase); GridFileStream gridFileStream = gridFile.OpenRead(filename); byte[] bytes = new byte[gridFileStream.Length]; gridFileStream.Read(bytes, 0, bytes.Length); return bytes; } private void GridFsDelete(string filename) { GridFile gridFile = new GridFile(mongoDatabase); gridFile.Delete(new Document("filename", filename)); }

   2. 再次封裝GridFS操作,新文件只儲存檔名稱,相當於只是一個鍵,新文件還可以有除“檔名”之外其他的鍵。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 /// <summary> /// 把圖片存到GridFS中 /// </summary> public void SaveImgGridFS(byte[] byteImg) { string filename = GridFsSave(byteImg); Document doc = new Document(); doc["ID"] = 1; doc["filename"] = filename; mongoCollection.Save(doc); } /// <summary> /// 獲取GridFS方式儲存的圖片 /// </summary> public byte[] GetImgGridFS() { Document doc = mongoCollection.FindOne(new Document { { "ID", 1 } }); string filename = doc["filename"].ToString(); return GridFsRead(filename); }

三、小結

  檔案存取應該不是很難,值得注意的地方是:用第一種方式從文件中讀出二進位制資料時,一定要將型別轉換為“Binary”型別;還有系統自帶的鍵“_id”,它也不是string型別,是“Oid”型別的。