Connection database: Reference address: https://blog.oz-code.com/how-to-mongodb-in-c-part-2/
//Empty ctor would get you a//client with a default localhost and port #27017Mongoclient m =Newmongoclient (); //---------------------------------------------------------------------- //Using a connection-stringMongoclient M1 =NewMongoclient ("mongodb://localhost:27017"); //---------------------------------------------------------------------- //Using mongoclientsettingsMongoclient client =NewMongoclient (Newmongoclientsettings {Server=NewMongoserveraddress ("localhost",27018), //Giving 3 seconds for a MongoDB server to is up before we throwServerselectiontimeout = Timespan.fromseconds (3) }); //---------------------------------------------------------------------- //Using MongourlMongoclient client1 =Newmongoclient (Mongourl.create ("mongodb://localhost:27017")); }
Curd:
Mongoclient mongoclient =NewMongoclient (configuration["Databaseconnection:0:host"]); varDbAdmin = Mongoclient.getdatabase ("Shoppingmall"); varBuyercollection = dbadmin.getcollection<buyer> ("Shoppingmall.buyer"); //New ////var a= buyercollection.insertoneasync (new Buyer {buyername= "admin", Buyerage=23, buyeraddress= "No. No. 067 Tianfu Avenue, Jianghan City", buyersex= ' 0 ', buyeridcard= "12355544884474", buyerheadportrait= "~/images/admin.jpg", buyeraccountnumber= "Admin1"}) ; //Enquiry//LINQ notation//var sql = from BB in buyercollection.asqueryable ()//Select BB; //sql= SQL. Where (t = t.buyerage >= 22); //var retlist = sql. ToList (); //EF notation//var ret = buyercollection.asqueryable (). GroupBy (t=>new {t.buyerage}). Where (t=>t.key.buyerage>=22); //var b = ret. ToList (); varFilterbuilder = builders<buyer>. Filter; varFilter = FILTERBUILDER.GT ("Buyerage",Ten) & Filterbuilder.lt ("counter", -) & Filterbuilder.lt ("counter", -);//Multiple conditions//var filter = Builders<buyer>. FILTER.GT ("Buyerage", 1);//a condition varDocument =awaitBuyercollection.findasync (filter); varres =document. ToList (); //Update varUpdatefilter = FILTERBUILDER.GT ("Buyerage", -) & Filterbuilder.lt ("Buyerage", -);//Multiple conditions varUpdated = Builders<buyer>. Update.set ("Buyername","John Doe"); varresult =Buyercollection.updatemanyasync (Updatefilter, updated). Result; //Delete varDelfilter = FILTERBUILDER.GT ("Buyerage", -) & Filterbuilder.lt ("Buyerage", -);//Multiple conditions varResut = Buyercollection.deletemanyasync (delfilter). Result;
which
configuration["Databaseconnection:0:host") belongs to. Netcore connection string notation. The specific implementation is as follows:
Appsettings.json:
" databaseconnection " : [ { "host""mongodb://localhost:27017" } ]
Read method:
Public iconfigurationroot configuration; Public Basecontroller () { // read appsettings configuration var New Configurationbuilder () . Setbasepath (Directory.GetCurrentDirectory ()) . Addjsonfile ("appsettings.json"); = Builder. Build (); }
MongoDB's connection to C # and curd notation