Mongodb儲存讀取Word文檔,mongodbword文檔

來源:互聯網
上載者:User

Mongodb儲存讀取Word文檔,mongodbword文檔

      在為人事系統做動作記錄功能時,為了保證已經列印的信函可以還原,需要在每次列印信函時記錄Word信函的內容。

   SQL Server只能記錄信函的文字內容,那信函的頁面配置、字型格式等其他內容如何儲存呢?此時Mongodb閃亮登場,由於MongoDB的文檔結構為BJSON格式(BJSON全稱:Binary JSON),而BJSON格式本身就支援儲存二進位格式的資料,因此可以把檔案的二進位格式的資料直接儲存到MongoDB的文檔結構中。取的時候再以二進位格式取,這樣文檔就能實現無損儲存。

   下面是我已經驗證成功,儲存Word到Mongo,然後從Mongo讀取Word的代碼,在此和大家分享分享。

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using MongoDB.Bson;using MongoDB.Driver;namespace Mongodb{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            Init();        }        //資料庫連接字串        const string strconn = "mongodb://127.0.0.1:27017";        //資料庫名稱        const string dbName = "test";        MongoServer server;        MongoDatabase db;        void Init()        {            //建立資料庫連結            server = MongoDB.Driver.MongoServer.Create(strconn);            //獲得資料庫            db = server.GetDatabase(dbName);        }        private void btnSave_Click(object sender, EventArgs e)        {            SaveDocToMongo(@"d:\quwenzhe.docx");        }        private void btnShow_Click(object sender, EventArgs e)        {            GetDocFromMongo(@"E:\newquwenzhe.doc");        }        /// <summary>        /// 儲存Word到Mongo        /// </summary>        /// <param name="filename">需要儲存的檔案名稱</param>        private void SaveDocToMongo(string filename)        {            byte[] byteDoc = File.ReadAllBytes(filename);            BsonDocument doc = new BsonDocument();            doc["id"] = "1";            doc["content"] = byteDoc;            MongoCollection col = db.GetCollection("doc");            col.Save(doc);        }        /// <summary>        /// 將Mongo中的Word儲存到本地        /// </summary>        /// <param name="filename">儲存到本地的檔案名稱</param>        private void GetDocFromMongo(string filename)        {            MongoCollection col = db.GetCollection("doc");            var query = new QueryDocument { { "id", "1" } };            var result = col.FindAs<BsonDocument>(query);            byte[] buff = (byte[])((BsonDocument)result.ToList()[0]).GetValue("content");            FileStream fs;            FileInfo fi = new FileInfo(filename);            fs = fi.OpenWrite();            fs.Write(buff, 0, buff.Length);            fs.Close();        }    }}
    執行完儲存操作後,大家可以在MongoVUE中查看儲存的位元據,如所示:
    

     到此大功告成,弱弱的奉上源碼:http://pan.baidu.com/s/1pJ5DTer。

     好了,時間不早了,我得小憩一下,準備下午的軟考,謝謝大家觀看。


想MongoDB這類NoSql,是不是只可以儲存文本資訊而不可以儲存二進位資訊?

MongoDB是文檔儲存型資料庫。它的儲存是給予作業系統中的檔案儲存體系統的。所以只要是檔案系統可以存的,mongodb都可以存。

如果要儲存一些二進位的大資料檔案,可以用GridFS資料結構。
 
Mongodb的Gridfs隱藏檔出現了一個異常

這個是由於同一系統下網域名稱改變或者不同系統下用了同一個網域名稱導致不同執行個體訪問出錯:
改變網域名稱後,重啟下所有mongod/mongos執行個體,應該就可以了。
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.