C # Implementation of MongoDB's simple additions and deletions to search and change

Source: Internet
Author: User

First add the required driver package (available through NuGet)

Using Mongodb.bson;
Using Mongodb.driver;
Using MongoDB.Driver.Builders;

First, set the configuration information

  //Connection Information        Static stringconn ="Mongodb://localhost"; Static stringDatabase ="Test"; Static stringCollection =" Person"; StaticMongoserver MongoDB = mongoserver.create (conn);//connecting to a database        StaticMongodatabase db = MongoDB. Getdatabase (database);//Select a database name        Staticmongocollection MC = db. GetCollection (collection);//Select Collection, equivalent to table

Second, insert the database

 Public Static void Insert (person p) {MC. Insert (P);}  Public Static void Insert (bsondocument b)  {MC. Insert (b);}

Third, update the database

    Public Static void Update (person p)        {            = bsonextensionmethods.tobsondocument (p);             = Query.eq ("_id", p._id);             New UpdateDocument (BD));        }          Public Static void Update (querydocument q,updatedocument u)        {            MC. Update (q, u);        }

Iv. Deleting a record

 Public Static void Delete (int  ID) {     mc. Remove (Query.eq ("_id", id));}

V. Querying the database

         Public StaticPerson Find (intID) {returnMc. Findoneas<person> (Query.eq ("_id", id)); }         Public StaticMongocursor<person>FindAll () {returnMc. Findallas<person>(); }         Public StaticMongocursor<person>Select (querydocument q) {returnMc. Findas<person>(q); }

VI. Number of statistical data

 Public Static Long Count (querydocument q)  {       return  MC. Count (q); }

Vii. Sorting and paging

    Public Static Mongocursor<person> skipandlimit (intint  b)        {            return Mc. Findallas<person>(). Setskip (a). Setlimit (b);        }

Viii. Applications and examples

   Static voidMain (string[] args) {MongoDB.            Connect (); //var person = Find (124); //Console.WriteLine (person.            Name); //Person .            Name = "Guizhu"; //Update (person); //Console.WriteLine (person. Name);            varquery =Newquerydocument {{"_id", -} }; varUpdate =Newupdatedocument {{"$set",Newquerydocument {{"PassWord","AAAAA" } } } };            Update (query,update); //Object Insertion//Person p = new Person {_id = n, Name = "Hello", PassWord = "4444"}; //Insert (P); //bsondocument Insertion//bsondocument B = new Bsondocument (); //B.add ("_id", 13); //B.add ("Name", "World"); //B.add ("PassWord", "6666"); //Insert (b); //var p= FindAll (); //foreach (var person in P)//{            //Console.WriteLine (person.            Name); //}            //querydocument query = new querydocument (); //bsondocument B = new Bsondocument (); //B.add ("$gte", 123); //B.add ("$lt", 125); //query.            ADD ("_id", b); //var p=select (query); //foreach (var person in P)//{            //Console.WriteLine (person.            Name); //}            //Console.WriteLine (Count (query)); //fieldsdocument f = new fieldsdocument (); //F.add ("Name", 1); //mongocursor<person> m = mc. Findas<person> (query).            Setfields (f); //foreach (var person in P)//{            //Console.WriteLine (person.            Name); //}            //sortbydocument s = new sortbydocument (); //S.add ("_id", 1);//-1=desc//var p = Sort (s); //foreach (var person in P)//{            //Console.WriteLine (person.            Name); //}            //var p = skipandlimit (1, 3); //foreach (var person in P)//{            //Console.WriteLine (person.            Name); //}                        //Delete (n);Console.WriteLine ("Complete");        Console.ReadLine (); }

  class person    {        publicint  _id;          Public string Name;          Public string PassWord;    }

C # Implementation of MongoDB's simple additions and deletions to search and change

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.