C # mongodb [lower],

Source: Internet
Author: User

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

 

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.