Preface: Daughter-in-law asked to be sent up. I want the first page of this article.
1,nuget introduced MongoDB. Then the namespace
Using mongodb.bson;using mongodb.driver;using mongodb.driver.builders;using MongoDB.Driver.Linq;
2, Global Declaration
Mongodatabase MONGO = new Mongoclient (configurationmanager.appsettings["Psconnstrmongodb"]). Getserver (). Getdatabase ("Psnew"); Mongocollection arts = NULL;
3, instantiation
Arts = MONGO. Getcollection<articleformongo> ("Artnew");
4, organize list, batch plug. Addictive interpolation
List<articleformongo> artins = ai. Select (e = new Articleformongo{intarticleid = Convert.ToInt32 (e.intarticleid),... Intfree = Convert.ToInt32 (E.intfree)}). ToList ();//Batch arts. Insertbatch (typeof (Articleformongo), artins);
5, organization ID List, batch Delete
foreach (Articlelist ali in AD) { sd. Append (Ali. Intarticleid + ","); } deletearticle (SD. ToString (). TrimEnd (', '));
Batch Delete
list<imongoquery> listquery = new list<imongoquery> (); string[] aidlist = aid. Split (', '); foreach (string ID in aidlist) { Listquery.add (Query.eq ("_id", Convert.ToInt32 (ID)));} Imongoquery query = query.or (listquery); Writeconcernresult result = Arts. Remove (query); bool BL = result! = NULL && result. Ok;
6, change
public bool Updatearticle (Articleformongo arm) {imongoquery query;query = Query.and (Query.eq ("_id", arm. Intarticleid)); Bsondocument bsondocument = arm. Tobsondocument<articleformongo> (); Bsondocument.remove ("_id"); var update = new UpdateDocument {"$set", Bsondocument}}; Writeconcernresult result = Arts. Update (query, update); bool BL = result! = NULL && result. ok;//if (BL)//{//common.appendtxtfile (Opearfilepath, "Modify success ID:" + arm.) Intarticleid);//}if (!BL) {common.appendtxtfile (Opearfilepath, "Modify failure ID:" + arm. Intarticleid);} return BL;}
7, check
8, Index
9, performance optimization
....
MongoDB C # CRUD (3)