public class Mongodbhelper {//define MONGO service private mongoserver MONGO = NULL; get databasename corresponding database, do not exist automatically create private mongodatabase mongodatabase; <summary>//Mongo database connection///</summary> public mongodbhelper () {m Ongo = Mongoserver.create (mongodbconfig.gconnectionstring); Mongodatabase = MONGO. Getdatabase (Mongodbconfig.gdatabasename) as mongodatabase; Mongo. Connect (); }///<summary>//Mongo database Disconnected///</summary> public void CloseConnection () {if (This.mongo! = null) {this.mongo.Disconnect (); This.mongo = null; }}///<summary>//Find all records according to conditions///</summary>//<returns></ret Urns> public ienumerable<bsondocument> FindAll (imongoquery pquery,int currentpage,int pagesize, StRing ptable) {//gets collectionname corresponding collection, does not exist automatically creates mongocollection<bsondocument> Mongocoll ection = mongodatabase.getcollection<bsondocument> (ptable) as mongocollection<bsondocument>; Return Mongocollection.find (pquery). Setlimit (pagesize). Setskip (pagesize * currentpage). ToList (); }//<summary>//Find all records///</summary>//<returns></returns> Public ienumerable<bsondocument> FindAll (int currentpage,int pagesize,string ptable) {//Get Coll Ectionname the corresponding collection, does not exist automatically creates mongocollection<bsondocument> mongocollection = Mongodatabase.getcollection<b Sondocument> (ptable) as mongocollection<bsondocument>; Return Mongocollection.findall (). Setlimit (pagesize). Setskip (Pagesize*currentpage). ToList (); }///<summary>//Find all records according to conditions///</summary>//<returns></returnS> public ienumerable<bsondocument> FindAll (imongoquery pquery, String ptable) {//Get COL Lectionname the corresponding collection, does not exist automatically creates mongocollection<bsondocument> mongocollection = mongodatabase.getcollection< Bsondocument> (ptable) as mongocollection<bsondocument>; Return Mongocollection.find (Pquery); }//<summary>//Find all records///</summary>//<returns></returns> Public ienumerable<bsondocument> FindAll (string ptable) {//gets collectionname corresponding collection, does not exist automatically created mongocollection<bsondocument> mongocollection = mongodatabase.getcollection<bsondocument> (pTable) as Mo ngocollection<bsondocument>; return Mongocollection.findall (); }///<summary>///Add a record///</summary>//<param name= "Doc" ></PARAM&G T public void Add (Object obj, string ptable) { Gets the CollectionName corresponding collection, does not exist automatically creates mongocollection<bsondocument> mongocollection = Mongodatabase.getcollection<bsondocument> (ptable) as mongocollection<bsondocument>; Mongocollection.insert (obj); }///<summary>///delete a record///</summary> public void Delete (string id, String pTa BLE) {//gets collectionname corresponding to the collection, does not exist automatically creates mongocollection<bsondocument> MONGO Collection = mongodatabase.getcollection<bsondocument> (ptable) as mongocollection<bsondocument>; Mongocollection.remove (new Querydocument {{"_id", ID}}); #region get the specified collection of the current connection database "by type"///<summary>//Get the specified collection of current connection database "by type"///</SUMMARY&G T <typeparam name= "T" ></typeparam>///<returns></returns> public mongocollection& Lt T> getcollection<t> (String name,writeConcern Writeconcern) where T:class {return this.mongodatabase.getcollection<t> (name,writeconce RN); }///<summary>///Get the specified collection of current connection database "according to the specified name"///</summary>//<typeparam name= " T "></typeparam>//<param name=" Name "> Collection name </param>//<returns></returns> Public mongocollection<t> getcollection<t> (string ptablename) where T:class {Retu RN this.mongodatabase.getcollection<t> (ptablename); } #endregion #region Gridfs file processing///<summary>//Save 2 binary data into DB///</summar y>//<param name= "Bytefile" ></param>///<returns></returns> public string G Ridfssave (byte[] bytefile) {string filename = Guid.NewGuid (). ToString (); Here the Gridfile constructor has an overload, and the bucket parameter is used to replace the default "FS" in the Create collection name. Mongogridfs gridfile = nEW Mongogridfs (mongodatabase); using (Mongogridfsstream Gridfilestream = gridfile.create (filename)) {Gridfilestream.write (byte File, 0, bytefile.length); } return filename; } public void Savegridfsfile (Bsondocument doc, string ptable) {Mongocollection<bsondo Cument> mongocollection =mongodatabase.getcollection<bsondocument> (ptable) as MongoCollection< bsondocument>; Mongocollection.save (DOC); }///<summary>///Read file as filename///</summary>//<param name= "filename" ;</param>//<returns></returns> public byte[] Gridfsread (string filename) { Mongogridfs gridfile = new Mongogridfs (mongodatabase); Mongogridfsstream gridfilestream = gridfile.openread (filename); byte[] bytes = new Byte[gridfilestream.length]; GRidfilestream.read (bytes, 0, bytes. Length); return bytes; }///<summary>//////</summary>//<param name= "Fitter" ></p aram>//<param name= "ptable" ></param>///<returns></returns> public Bso Ndocument Getfsfileinfo (querydocument Fitter, string ptable) {mongocollection<bsondocument> mong Ocollection = mongodatabase.getcollection<bsondocument> (ptable) as mongocollection<bsondocument>; Bsondocument doc = Mongocollection.findone (fitter); return doc; }///<summary>//delete files///</summary>//<param name= "filename" ></para m> public void Gridfsdelete (string filename) {Mongogridfs gridfile = new Mongogridfs (mongodatabase); Gridfile.delete (New querydocument ("filename", filename)); } #endregion}
Note: Since I recently developed a project using. NET, I have the opportunity to use MongoDB, I changed myself to a generic class
Method:
1. Find MongoDB website
2. Download the Mongodb.net class library, I use Mongodb.bson.dll,mongodb.driver.dll version 1.7, each version is not the same .... MongoDB support for language access is being enhanced.
3. The above is the underlying code
[Summary].net Operation MongoDB General base class 1: