C # MongoDB [Next]

Source: Internet
Author: User
Tags mongoclient

Overview

The traditional relational database is usually composed of three hierarchical concepts of database, table, record, and MongoDB is made up of database, collection (collection), Three levels of document objects. MongoDB is for tables in relational databases, but there is no concept of columns, rows, and relationships in the collection, which embodies the nature of pattern freedom.

In C # want to use MongoDB we also need to download the driver, now generally more common drivers have official website drive, Samus Drive. The Samus driver supports LINQ-style manipulation of data in addition to general-form operations. This article is used by the official website driver. Official website Drive address: Click to download

After downloading, we can get two DLLs in the download package:

MongoDB.Driver.dll drivers

MongoDB.Bson.dll serialization, JSON-related

Instance Code

New:

//Database connection StringConst stringConnectionString ="mongodb://127.0.0.1:27017";//DatabaseConst stringDatabase ="ck_test_db"; Mongoclient Client=Newmongoclient (connectionString); Mongoserver Server=client. Getserver (); Mongodatabase DB=server. Getdatabase (Database); Mongocollection Collection= db. GetCollection ("Student");//NewStudent Stud_add =NewStudent () {name="Zhao Yun", code="2001", sex="female", age=" at"};collection. Insert<student>(stud_add);//EnquiryQuerydocument Query_sel =Newquerydocument {{"name","Zhao Yun" } };varList = collection. Findas<student>(Query_sel);foreach(Student iteminchlist) {Console.WriteLine ("Name:{0},code:{1}", Item.name, Item.code);}

Show Results:

Modify:

//Database connection StringConst stringConnectionString ="mongodb://127.0.0.1:27017";//DatabaseConst stringDatabase ="ck_test_db"; Mongoclient Client=Newmongoclient (connectionString); Mongoserver Server=client. Getserver (); Mongodatabase DB=server. Getdatabase (Database); Mongocollection Collection= db. GetCollection ("Student");//UpdateQuerydocument QUERY_UPD =Newquerydocument {{"name","Zhao Yun"}};imongoupdate Update= Update.set ("Code","4444"); Collection. Update (QUERY_UPD, update);

Show Results:

Delete :

//Database connection StringConst stringConnectionString ="mongodb://127.0.0.1:27017";//DatabaseConst stringDatabase ="ck_test_db"; Mongoclient Client=Newmongoclient (connectionString); Mongoserver Server=client. Getserver (); Mongodatabase DB=server. Getdatabase (Database); Mongocollection Collection= db. GetCollection ("Student");//DeleteQuerydocument Query_del =Newquerydocument {{"name","Zhao Yun"}};collection. Remove (Query_del);

Full-Volume code:

Static voidMain (string[] args) {    //Database connection String    Const stringConnectionString ="mongodb://127.0.0.1:27017"; //Database    Const stringDatabase ="ck_test_db"; Mongoclient Client=Newmongoclient (connectionString); Mongoserver Server=client.    Getserver (); Mongodatabase DB=server.    Getdatabase (Database); Mongocollection Collection= db. GetCollection ("Student"); //NewStudent Stud_add =NewStudent () {name="Zhao Yun", code="2001", sex="female", age=" at" }; Collection. Insert<student>(Stud_add); //UpdateQuerydocument QUERY_UPD =Newquerydocument {{"name","Zhao Yun" } }; Imongoupdate Update= Update.set ("Code","4444"); Collection.    Update (QUERY_UPD, update); //DeleteQuerydocument Query_del =Newquerydocument {{"name","Zhao Yun" } }; Collection.    Remove (Query_del); //EnquiryQuerydocument Query_sel =Newquerydocument {{"name","Zhao Yun" } }; varList = collection. Findas<student>(Query_sel); foreach(Student iteminchlist) {Console.WriteLine ("Name:{0},code:{1}", Item.name, Item.code); }} Public classstudent{ PublicObjectId _id;//Bsontype.objectid this corresponds to MongoDB.Bson.ObjectId.     Public stringName {Get;Set; }  Public stringCode {Get;Set; }  Public stringSex {Get;Set; }  Public stringAge {Get;Set; } /*The _id property must be there, or the data will be updated with an error: "Element ' _id ' does not match any field or property of Class". */}
View Code

The basis of the deletion and modification is described above.

Instance code: Click to download

C # MongoDB [Next]

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.