C # mongodb [lower],
Overview
Traditional relational databases are generally composed of three levels: database, table, and record. MongoDB is composed of databases and collections) and document objects. MongoDB has no concept of columns, rows, and links for tables in relational databases, which reflects the free mode.
To use MongoDB in C #, we also need to download the driver. Currently, common drivers include the official website driver and the samus driver. In addition to common operations, the samus driver also supports data manipulation in the linq mode. This article uses the official website driver. Official Website driver address: Click to download
After the download, we can get two DLL files in the downloaded package:
MongoDB. Driver. dll Driver
MongoDB. Bson. dll serialization, Json-related
Instance code
New:
// Database connection string const string connectionString = "mongodb: // 127.0.0.1: 27017"; // Database const string Database = "ck_test_db"; connector client = new connector client (connectionString ); login server = client. getServer (); your database db = server. getDatabase (Database); Collect collection = db. getCollection ("student"); // added student stud_Add = new student () {name = "zhao Yun", code = "2001", sex = "female ", age = "23"}; collection. insert <student> (stud_Add); // query QueryDocument query_sel = new QueryDocument {"name", "Zhao Yun" }}; var list = collection. findAs <student> (query_sel); foreach (student item in list) {Console. writeLine ("name: {0}, code: {1}", item. name, item. code );}
Display result:
Modify:
// Database connection string const string connectionString = "mongodb: // 127.0.0.1: 27017"; // Database const string Database = "ck_test_db"; connector client = new connector client (connectionString ); login server = client. getServer (); your database db = server. getDatabase (Database); Collect collection = db. getCollection ("student"); // update QueryDocument query_upd = new QueryDocument {"name", "Zhao Yun" }}; IMongoUpdate Update = update. set ("code", "4444"); collection. update (query_upd, update );
Display result:
Delete:
// Database connection string const string connectionString = "mongodb: // 127.0.0.1: 27017"; // Database const string Database = "ck_test_db"; connector client = new connector client (connectionString ); login server = client. getServer (); your database db = server. getDatabase (Database); Collect collection = db. getCollection ("student"); // Delete QueryDocument query_del = new QueryDocument {"name", "Zhao Yun"}; collection. remove (query_del );
Full code:
Static void Main (string [] args) {// Database connection string const string connectionString = "mongodb: // 127.0.0.1: 27017"; // Database const string Database = "ck_test_db "; connecting client = new connecting client (connectionString); Connecting server = client. getServer (); your database db = server. getDatabase (Database); Collect collection = db. getCollection ("student"); // added student stud_Add = new student () {name = "zhao Yun ", Code =" 2001 ", sex =" female ", age =" 23 "}; collection. insert <student> (stud_Add); // update QueryDocument query_upd = new QueryDocument {"name", "Zhao Yun" }}; IMongoUpdate Update = update. set ("code", "4444"); collection. update (query_upd, update); // Delete QueryDocument query_del = new QueryDocument {"name", "Zhao Yun"}; collection. remove (query_del); // query QueryDocument query_sel = new QueryDocument {"name", "Zhao Yun" }; Var list = collection. findAs <student> (query_sel); foreach (student item in list) {Console. writeLine ("name: {0}, code: {1}", item. name, item. code) ;}} public class student {public ObjectId _ id; // BsonType. objectId corresponds to MongoDB. bson. objectId public string name {get; set;} public string code {get; set;} public string sex {get; set;} public string age {get; set ;} /* _ id attribute must be available; otherwise, an error will be reported during data update:" Element '_ id' does not match any field or property of class ". */}View Code
Basic addition, deletion, modification, and query are described above.
Instance code: Click to download