0) Basic Preparation
Install MongoDB driver (current article is based on MongoDB.Driver.dll, v1.7.0.4714)
Using mongodb.bson;using mongodb.driver;
1) Create a connection
#0) Prepare the connection string and dbprivate static readonly string _connectionstring = "Mongodb://localhost";p rivate static readonly string _ DbName = "Test";//1) Create Connectionvar _mongoclient = new Mongoclient (_connectionstring); var _server = _ Mongoclient.getserver (); var _db = _server. Getdatabase (_dbname); var collection = _db. GetCollection ("Redpack");
2) Implementation of additions and deletions
dictionary<string, string> contact = new Dictionary<string, string>;contact. ADD ("Mch_billno", "12345678"); ADD ("Pername", "King Mo"); contact. ADD ("Sex", "male"); ADD ("Age", "35"),//2) Insert Bsondocument doc = new bsondocument ();d OC. Setdocumentid (contact["Mch_billno"]);//In order to achieve their own maintenance doc. ADD (Contact); collection. Insert (DOC);//3) update var query = new Querydocument {{"_id", contact["Mch_billno"]}};var Updatebson = new Bsondocument (); u Pdatebson.add (Contact), var updatedocument = new UpdateDocument {"$set", Updatebson}};collection. Update (query, updatedocument);
. NET using MongoDB first experience