MongoDB C # CRUD (3)

Source: Internet
Author: User

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)

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.