The previous article explains how to use MongoDB for replication, but I use BSON Object to save files. This limitation is that if the file size exceeds 4 MB, an exception occurs.
According to the official website,BSON objects in MongoDB are limited to 4 MB in size.Http://www.mongodb.org/display/DOCS/GridFS
Therefore, I re-wrote the operation class and used GridFS to save the file. The code was very simple, but it took a long time for me to get started with the operation class and there was a problem that could not be solved, I want to generate a Guid's _ id instead of the _ id generated by mongodb, but it cannot be solved all the time. Which of the following experts can give me some advice? Thank you!
This is written on the client provided by the Mongodb Official Website:
using System;using System.Collections.Generic;using System.Text;using System.IO;using MongoDB.Driver;using MongoDB.Driver.Builders;using MongoDB.Bson;using MongoDB.Driver.GridFS;namespace FileUtility{ public class DocUtility { private string connStr = ""; public string ConnStr { get { if (string.IsNullOrEmpty(connStr)) { throw new ArgumentNullException("Connection string did not specify!"); } return connStr; } } public DocUtility() { connStr = System.Configuration.ConfigurationManager.AppSettings["FileDb"]; } public DocUtility(string connectionString) { this.connStr = connectionString; } /// <summary> /// add a document to mongodb /// </summary> /// <param name="content">document bytes array</param> /// <returns>the unique identity filename in mongodb</returns> public string AddDoc(byte[] content) { MongoServer server = MongoServer.Create(this.ConnStr); try { string filename = Guid.NewGuid().ToString(); server.Connect(); MongoDatabase db = server.GetDatabase("ecDocs"); MongoGridFS fs = new MongoGridFS(db, new MongoGridFSSettings() { Root="ecDocs" }); MongoGridFSFileInfo info = new MongoGridFSFileInfo(fs, filename); using (MongoGridFSStream gfs = info.Create()) { gfs.Write(content, 0, content.Length); } return filename; } catch (Exception ex) { throw ex; } finally { if (server != null) server.Disconnect(); } } /// <summary> /// delete doc from mongodb /// </summary> /// <param name="filename">the unique identity filename</param> public string DeleteDoc(string filename) { MongoServer server = MongoServer.Create(this.ConnStr); try { server.Connect(); MongoDatabase db = server.GetDatabase("ecDocs"); MongoGridFS fs = new MongoGridFS(db, new MongoGridFSSettings() { Root = "ecDocs" }); fs.Delete(filename); return filename; } catch (Exception ex) { throw ex; } finally { if (server != null) server.Disconnect(); } } /// <summary> /// get document bytes array from mongodb /// </summary> /// <param name="filename">unique filename</param> /// <returns>bytes array</returns> public byte[] GetDoc(string filename) { MongoServer server = MongoServer.Create(this.ConnStr); try { server.Connect(); MongoDatabase db = server.GetDatabase("ecDocs"); MongoGridFS fs = new MongoGridFS(db,new MongoGridFSSettings() { Root="ecDocs" }); byte[] bytes = null; using (MongoGridFSStream gfs = fs.Open(filename, FileMode.Open)) { bytes = new byte[gfs.Length]; gfs.Read(bytes, 0, bytes.Length); } return bytes; } catch (Exception ex) { throw ex; } finally { if (server != null) server.Disconnect(); } } }}
The following class uses the Samus client:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using MongoDB;using MongoDB.GridFS;namespace FileUtilitySamus{ public class DocUtility { private string connStr = ""; public string ConnStr { get { if (string.IsNullOrEmpty(connStr)) { throw new ArgumentNullException("Connection string did not specify!"); } return connStr; } } public DocUtility() { connStr = System.Configuration.ConfigurationManager.AppSettings["FileDb"]; } public DocUtility(string connectionString) { this.connStr = connectionString; } /// <summary> /// add a document to mongodb /// </summary> /// <param name="content">document bytes array</param> /// <returns>the unique identity filename in mongodb</returns> public string AddDoc(byte[] content) { using (Mongo mongo = new Mongo(this.ConnStr)) { try { string filename = Guid.NewGuid().ToString(); mongo.Connect(); IMongoDatabase db = mongo.GetDatabase("ecDoc"); GridFile fs = new GridFile(db, "ecDoc"); GridFileInfo info = new GridFileInfo(db, "ecDoc", filename); using (GridFileStream gfs = info.Create()) { gfs.Write(content, 0, content.Length); } return filename; } catch (Exception ex) { throw ex; } } } /// <summary> /// delete doc from mongodb /// </summary> /// <param name="filename">the unique identity filename</param> public void DeleteDoc(string filename) { using (Mongo mongo = new Mongo(this.ConnStr)) { try { mongo.Connect(); IMongoDatabase db = mongo.GetDatabase("ecDoc"); GridFile fs = new GridFile(db, "ecDoc"); fs.Delete(new Document("filename", filename)); } catch (Exception ex) { throw ex; } } } /// <summary> /// get document bytes array from mongodb /// </summary> /// <param name="filename">unique filename</param> /// <returns>bytes array</returns> public byte[] GetDoc(string filename) { using (Mongo mongo = new Mongo(this.ConnStr)) { try { mongo.Connect(); IMongoDatabase db = mongo.GetDatabase("ecDoc"); GridFile fs = new GridFile(db, "ecDoc"); GridFileStream gfs = fs.OpenRead(filename); byte[] bytes = new byte[gfs.Length]; gfs.Read(bytes, 0, bytes.Length); return bytes; } catch (Exception ex) { if (ex.GetType() == typeof(System.IO.DirectoryNotFoundException)) throw new System.IO.FileNotFoundException("File not found :" + filename); else throw ex; } } } }}
I don't know what the performance of the two clients is like? I hope you can share it with me.