Mongodb official website driver 2.2.4.26 version add, delete, modify, query, mongodb2.2.4.26

Source: Internet
Author: User
Tags mongodb version

Mongodb official website driver 2.2.4.26 version add, delete, modify, query, mongodb2.2.4.26

Mongodb version 3.2.7

 

I recently did not find any mongodb database on the Internet. earlier Than Version X, because the driver from 2. X has been modified a lot later, and previously does not support the current 2. X also supports,

 

Start the Mongodb service. Now let's take a look at the basic operations.

Code:

First, define a model to test the operation.

Public class Persion {// reference using MongoDB. bson. serialization. attributes; [BsonId] public string _ id; public string Name; public int age; public string Email; public string TableName {get {return "persion ";}}}

Add the mongob C # driver to the reference. You can use Nuget to add the reference method or directly download it from the official website and add it to the project,

Configure and create mongodbClient. Note: [the previous version is MongoServer] and complete the configuration.

The address can be remotely or locally used.

// Database connection string static string conn = "mongodb: // 127.0.0.1: 27017"; // database name static string database = "appdb "; // database name static response url = new response url (conn); static response client ci = new response client (url); // create response client static IMongoDatabase db = ci. getDatabase (database); // obtain the database

The following describes how to operate data:

// Add public static void InsertUser (Persion per = null) {// link to the table. It does not matter if the table does not exist in the database, otherwise, mongodb will automatically create a Collection // ("persion") Collection, also called table var colls = db. getCollection <Persion> ("persion"); per = new Persion {_ id = Guid. newGuid (). toString (), Name = "ynland@126.com", age = 12}; colls. insertOne (per);} // modify public static void Updat () {// obtain data var colls = db. getCollection <Persion> ("persion"); var model = Colls. Find (c => c. Name = "ynland"). FirstOrDefault (); // filter data // condition. It is equivalent to the Filte after where. eq is equivalent to equal sign Filter. eq ("field", matching value) var filter = MongoDB. driver. builders <Persion>. filter. eq ("Name", model. name); // modified value, set ("field", "modified value") Note that the field must match the field in the database, if no match is found, a new field is automatically created and the value var update = MongoDB is assigned. driver. builders <Persion>. update. set ("age", "66"); // colls. updateOne (filter, update); // modify an UpdateResult res = colls. updateMany (filter, update); // multiple modifications} public static void DeleteInfo () {var colls = Db. getCollection <Persion> ("persion"); colls. deleteOne (c => c. age = 100);} // obtain the public static void GetAllInfo () {// the data type must be consistent to not serialize var t = db. getCollection <Persion> ("persion "). find (c => c. name! = String. Empty); List <Persion> list = t. ToList ();}

Filtering during modification, and set convenience

Filter. XXX ("field name", "companion value ").

Set (). set ()...

 

All code:

Using System; using System. collections. generic; using System. linq; using System. web; using MongoDB. driver; using MongoDB. bson; using MongoDB. driver. linq; using MongoDB. bson. serialization. attributes; namespace using test1 {public static class using unitily {// database connection string static string conn = "mongodb: // 127.0.0.1: 27017 "; // database name static string database = "appdb"; // database name static response url = new response url (conn); st Atic Consumer Client ci = new Consumer Client (url); // create consumer client static IMongoDatabase db = ci. getDatabase (database); // obtain the database // Add public static void InsertUser (Persion per = null) {// link to the table. It does not matter if the table does not exist in the database, otherwise, mongodb will automatically create a Collection // ("persion") Collection, also called table var colls = db. getCollection <Persion> ("persion"); per = new Persion {_ id = Guid. newGuid (). toString (), Name = "ynland@126.com", age = 12}; colls. I NsertOne (per);} // modify public static void Updat () {// obtain data var colls = db. getCollection <Persion> ("persion"); var model = colls. find (c => c. name = "ynland "). firstOrDefault (); // filter data // condition. It is equivalent to the Filte after where. eq is equivalent to equal sign Filter. eq ("field", matching value) var filter = MongoDB. driver. builders <Persion>. filter. anyEq ("Name", model. name); // modified value, set ("field", "modified value") Note that the field must match the field in the database, if no match is found, a new field is automatically created and the value var update = MongoDB is assigned. driver. builders <Persion>. update. set ("age", "66"); // colls. updateOne (filter, update); // modify an UpdateResult res = colls. updateMany (filter, update); // multiple modifications} public static void DeleteInfo () {var co Lls = db. getCollection <Persion> ("persion"); colls. deleteOne (c => c. age = 100);} // obtain the public static void GetAllInfo () {// the data type must be consistent to not serialize var t = db. getCollection <Persion> ("persion "). find (c => c. name! = String. empty); List <Persion> list = t. toList () ;}} public class Persion {// reference using MongoDB. bson. serialization. attributes; [BsonId] public string _ id; public string Name; public int age; public string Email; public string TableName {get {return "persion ";}}}View Code

 

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.