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]