MongoDB's connection to C # and curd notation

Source: Internet
Author: User
Tags mongoclient mongodb server

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.