[Summary]. net operations for MongoDb common basic class 1:. netmongodb
Public class MongoDBHelper {// defines the Mongo service private MongoServer mongo = null; // gets the database corresponding to databaseName. If no database exists, the private MongoDatabase will be created automatically; /// <summary> /// Mongo database connection // </summary> public MongoDBHelper () {mongo = MongoServer. create (MongoDBConfig. gConnectionString); connected database = mongo. getDatabase (MongoDBConfig. gDatabaseName) as your database; mongo. connect () ;}/// <summary> // Mongo data Database disconnection // </summary> public void CloseConnection () {if (this. mongo! = Null) {this. mongo. disconnect (); this. mongo = null ;}} /// <summary> /// search for all records based on conditions /// </summary> /// <returns> </returns> public IEnumerable <BsonDocument> FindAll (IMongoQuery pQuery, int currentpage, int pagesize, string pTable) {// gets the collection corresponding to collectionName. If the collection does not exist, the custom collection is automatically created. <BsonDocument> Custom collection = custom database. getCollection <BsonDocument> (pTable) as your collection <BsonDocument>; ret Urn upload collection. find (pQuery ). setLimit (pagesize ). setSkip (pagesize * currentpage ). toList ();;} /// <summary> /// query all records /// </summary> /// <returns> </returns> public IEnumerable <BsonDocument> FindAll (int currentpage, int pagesize, string pTable) {// gets the collection corresponding to the collectionName. If no collection exists, the custom collection is automatically created. <BsonDocument> Custom collection = custom database. getCollection <BsonDocument> (pTable) as your collection <B SonDocument>; return response collection. findAll (). setLimit (pagesize ). setSkip (pagesize * currentpage ). toList ();} /// <summary> /// search for all records based on conditions /// </summary> /// <returns> </returns> public IEnumerable <BsonDocument> FindAll (IMongoQuery pQuery, string pTable) {// obtain the collection corresponding to the collectionName. If no collection exists, the custom collection is automatically created. <BsonDocument> Custom collection = custom database. getCollection <BsonDocument> (pTable) as your collect Ion <BsonDocument>; return response collection. find (pQuery );} /// <summary> /// query all records /// </summary> /// <returns> </returns> public IEnumerable <BsonDocument> FindAll (string pTable) {// obtain the collection corresponding to the collectionName. If no collection exists, the custom collection <BsonDocument> Custom collection = custom database is automatically created. getCollection <BsonDocument> (pTable) as your collection <BsonDocument>; return your collection. findAll ();} // <summary> // Add Add a record // </summary> // <param name = "doc"> </param> public void Add (object obj, string pTable) {// obtain the collection corresponding to the collectionName. If no collection exists, the custom collection <BsonDocument> Custom collection = custom database is automatically created. getCollection <BsonDocument> (pTable) as your collection <BsonDocument>; your collection. insert (obj) ;}//< summary> /// Delete a record /// </summary> public void Delete (string id, string pTable) {// obtain the corresponding collectionName If the collection does not exist, the custom collection <BsonDocument> Custom collection = custom database is automatically created. getCollection <BsonDocument> (pTable) as your collection <BsonDocument>; your collection. remove (new QueryDocument {"_ id", id }});} # region: Obtain the specified set of the current connected database [by type] /// <summary> /// obtain the specified set of the current connected database [by type] /// </summary> /// <typeparam name = "T"> </typeparam> // <returns> </returns> public collection <T> GetCollection <T> (strin G name, WriteConcern writeConcern) where T: class {return this. your database. getCollection <T> (name, writeConcern );} /// <summary> /// obtain the specified set of currently connected databases [Based on the specified name] /// </summary> /// <typeparam name = "T"> </typeparam> // <param name = "name"> set name </param> // <returns> </returns> public Collections collection <T> GetCollection <T> (string pTableName) where T: class {return this. your database. getCollection <T> (pTableN Ame );} # endregion # region GridFs File Processing // <summary> // Save the binary data to the db /// </summary> // <param name = "byteFile "> </param> /// <returns> </returns> public string GridFsSave (byte [] byteFile) {string filename = Guid. newGuid (). toString (); // here, the GridFile constructor has an overload. The bucket parameter is used to replace the default "fs" in the created set name. Repeated gridfs gridFile = new relational gridfs (relational database); using (relational gridfsstream gridFileStream = gridFile. create (filename) {gridFileStream. write (byteFile, 0, byteFile. length);} return filename;} public void SaveGridFsFile (BsonDocument doc, string pTable) {your collection <BsonDocument> your collection = your database. getCollection <BsonDocument> (pTable) as your collection <BsonDocument>; your collection. save (doc );} /// <summary> /// read the file named filename /// </summary> /// <param name = "filename"> </param> // <returns> </returns> public byte [] GridFsRead (string filename) {repeated gridfs gridFile = new relational gridfs (relational database); repeated gridfsstream gridFileStream = gridFile. openRead (filename); byte [] bytes = new byte [gridFileStream. length]; gridFileStream. read (bytes, 0, bytes. length); return bytes ;} /// <summary> /// obtain a data entry based on the condition /// </summary> /// <param name = "fitter"> </param> // <param name = "pTable"> </param> // <returns> </returns> public BsonDocument GetFsFileInfo (QueryDocument fitter, string pTable) {your collection <BsonDocument> your collection = your database. getCollection <BsonDocument> (pTable) as your collection <BsonDocument>; BsonDocument doc = your collection. findOne (fitter); return doc ;} /// <summary> /// delete a file /// </summary> /// <param name = "filename"> </param> public void GridFsDelete (string filename) {destination gridfs gridFile = new destination gridfs (destination database); gridFile. delete (new QueryDocument ("filename", filename) ;}# endregion}
Note: Due to the recent development of a project using. net, I have the opportunity to use mongodb and changed it to a common class.
Method:
1. Find the official mongodb website.
2. download the mongodb.net class library. I use MongoDB. bson. dll, MongoDB. driver. the dll version 1.7, and each version is different ..... mongodb's support for language access is being enhanced.
3. The above is the underlying code