zoukankan      html  css  js  c++  java
  • MongoDB学习笔记(五) MongoDB文件存取操作

    http://www.cnblogs.com/lipan/archive/2011/03/21/1989409.html

    由于MongoDB的文档结构为BJSON格式(BJSON全称:Binary JSON),而BJSON格式本身就支持保存二进制格式的数据,因此可以把文件的二进制格式的数据直接保存到MongoDB的文档结构中。但是由于一个 BJSON的最大长度不能超过4M,所以限制了单个文档中能存入的最大文件不能超过4M。为了提供对大容量文件存取的支持,samus驱动提供了 “GridFS”方式来支持,“GridFS”方式文件操作需要引入新的程序集“MongoDB.GridFS.dll”。下面我们分别用两种方式来实 现。

    一、在文档对象中存取文件

      当文件大小较小的时候,直接存入文档对象实现起来更简洁。比如大量图片文件的存取等,一般图片文件都不会超过4M。我们先实现一个上传图片存入数据库,再取出来写回页面的例子:

       1. 把图片存到BJSON中

    01 /// <summary>
    02 /// 把图片存到BJSON中
    03 /// </summary>
    04 public void SaveImgBJSON(byte[] byteImg)
    05 {
    06     Document doc = new Document();
    07     doc["ID"] = 1;
    08     doc["Img"] = byteImg;
    09     mongoCollection.Save(doc);
    10 }

       2. 获取BJSON方式存储的图片字节数据

    1 /// <summary>
    2 /// 获取BJSON方式存储的图片字节数据
    3 /// </summary>
    4 public byte[] GetImgBJSON()
    5 {
    6   Document doc=  mongoCollection.FindOne(new Document { { "ID", 1 } });
    7   return doc["Img"] as Binary;
    8 }

      上面两段代码是在对MongoDB相关操作进行BLL封装类中添加的两个方法,封装方式查看上节内容。下面看看在webform中如何调用:

      在界面拖出一个FileUpload控件和一个Button控件,页面cs类加如下方法:

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

    二、用GridFS方式存取文件

      在实现GridFS方式前我先讲讲它的原理,为什么可以存大文件。驱动首先会在当前数据库创建两个集合:"fs.files" 和"fs.chunks"集合,前者记录了文件名,文件创建时间,文件类型等基本信息;后者分块存储了文件的二进制数据(并支持加密这些二进制数据)。分 块的意思是把文件按照指定大小分割,然后存入多个文档中。"fs.files"怎么知道它对应的文件二进制数据在哪些块呢?那是因为 在"fs.chunks"中有个"files_id"键,它对应"fs.files"的"_id"。"fs.chunks"还有一个键(int 型)"n",它表明这些块的先后顺序。这两个集合名中的"fs"也是可以通过参数自定义的。

      如果你只是想知道怎么用,可以忽略上面这段话,下面将用法:

       1. GridFS方式的文件新建,读取,删除

    01 private string GridFsSave(byte[] byteFile)
    02 {
    03     string filename = Guid.NewGuid().ToString();
    04  
    05     //这里GridFile构造函数有个重载,bucket参数就是用来替换那个创建集合名中默认的"fs"的。
    06     GridFile gridFile = new GridFile(mongoDatabase);
    07     using (GridFileStream gridFileStream = gridFile.Create(filename))
    08     {
    09         gridFileStream.Write(byteFile, 0, byteFile.Length);
    10     }
    11     return filename;
    12 }
    13  
    14 private byte[] GridFsRead(string filename)
    15 {
    16     GridFile gridFile = new GridFile(mongoDatabase);
    17     GridFileStream gridFileStream = gridFile.OpenRead(filename);
    18     byte[] bytes = new byte[gridFileStream.Length];
    19     gridFileStream.Read(bytes, 0, bytes.Length);
    20     return bytes;
    21 }
    22  
    23 private void GridFsDelete(string filename)
    24 {
    25     GridFile gridFile = new GridFile(mongoDatabase);
    26     gridFile.Delete(new Document("filename", filename));
    27 }

       2. 再次封装GridFS操作,新文档只存储文件名称,相当于只是一个键,新文档还可以有除“文件名”之外其他的键。

    01 /// <summary>
    02 /// 把图片存到GridFS中
    03 /// </summary>
    04 public void SaveImgGridFS(byte[] byteImg)
    05 {
    06     string filename = GridFsSave(byteImg);
    07  
    08     Document doc = new Document();
    09     doc["ID"] = 1;
    10     doc["filename"] = filename;
    11     mongoCollection.Save(doc);
    12 }
    13  
    14 /// <summary>
    15 /// 获取GridFS方式存储的图片
    16 /// </summary>
    17 public byte[] GetImgGridFS()
    18 {
    19     Document doc = mongoCollection.FindOne(new Document { { "ID", 1 } });
    20     string filename = doc["filename"].ToString();
    21     return GridFsRead(filename);
    22 }

    三、小结

      文件存取应该不是很难,值得注意的地方是:用第一种方式从文档中读出二进制数据时,一定要将类型转换为“Binary”类型;还有系统自带的键“_id”,它也不是string类型,是“Oid”类型的。

  • 相关阅读:
    How to convert VirtualBox vdi to KVM qcow2
    (OK)(OK) adb -s emulator-5554 shell
    (OK)(OK) using adb with a NAT'ed VM
    (OK) How to access a NAT guest from host with VirtualBox
    (OK) Creating manually one VMs from an existing VDI file in CLI (VBoxManage) in Fedora 23
    (OK)(OK) Creating VMs from an existing VDI file in CLI (VBoxManage) in Fedora 23
    (OK) Creating_VMs_from_an_existing_VDI_file.txt
    (OK) Creating VMs from an existing VDI file —— in OS X
    (OK) install_IBM_SERVER.txt
    (OK) install chrome & busybox in android-x86_64 —— uninstall chrome
  • 原文地址:https://www.cnblogs.com/licheng/p/2241880.html
Copyright © 2011-2022 走看看