MONGODB official website Driver 2.2.4.26 version increase, deletion, check, mongodb2.2.4.26

Source: Internet
Author: User
Tags mongoclient serialization

MongoDB is 3.2.7 version

Recently in the Learning MongoDB database found on the Internet is not 2. X version of the following, since the drive from 2. X was modified a lot later, and LINQ now 2 is not supported. X is supported, too.

Mongodb Start-up service don't say it now. Basic operations

On the 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 Mongob C#driver to the reference, which can be added via NuGet or directly to the website and then added to the project.

Configuration creation Mongodbclient Note: "Previous version is Mongoserver" and ready to configure

The address can be either remote or local, and is now using the local

Database connection string        static string conn = "mongodb://127.0.0.1:27017";        Database name        static string database = "appdb";//Database name        static Mongourl url = new Mongourl (conn);        static Mongoclient ci = new mongoclient (URL);//Create mongoclient        static imongodatabase db = Ci. Getdatabase (database);//Get Databases

Here's how the data is done

Add data public static void Insertuser (persion per = null) {//Link to table, database does not exist in table okay, no mongodb will automatically create a C Ollection//("Persion") collection is also called table var colls = db.            Getcollection<persion> ("Persion"); per = new Persion {_id = Guid.NewGuid ().            ToString (), Name = "[email protected]", age = 12}; Colls.        Insertone (per); }//Modify public static void Updat () {//get data var colls = db.            Getcollection<persion> ("Persion"); var model = Colls. Find (c = c.name = = "Ynland"). FirstOrDefault ();//filter data//conditions. Equivalent to where the filte.eq equals the 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 matches the field in the database, the mismatch cannot be found to automatically create a new field and assign the value var update = Mongodb.driver.builders<persion>.           Update.set ("Age", "66"); Colls. Updateone (filter, update);//Modify a data updatEresult res= Colls.            Updatemany (filter, update);//Multiple modifications} public static void Deleteinfo () { var colls = db.            Getcollection<persion> ("Persion"); Colls.        Deleteone (c = c.age = = 100); }//Get data public static void Getallinfo () {///data type to be consistent to not serialize var t = db. Getcollection<persion> ("Persion"). Find (c = c.name!=string.            Empty);        list<persion> list = T.tolist (); }

Filter at the time of modification, and set convenient place

Filter the Criteria filter.xxx ("Field name", "companion value") to filter

Set (). Set (). Set (). Set (). Sets (). To assign a value like this

All code:

using system; using system.collections.generic; using system.linq; using  system.web;using mongodb.driver; using mongodb.bson; using mongodb.driver.linq;  Usingmongodb.bson.serialization.attributes; namespace mongotest1 { public static  classmongounitily { //database connection string  static string conn =  "mongodb://127.0.0.1:27017";  / /database name  static string database =  "appdb";//database name  static mongourl URL =  NEWMONGOURL (conn);  static mongoclient ci = new mongoclient (URL);//Create Mongoclient static  imongodatabase db = ci. Getdatabase (database);//Get  //Add Data  public static void insertuser (persion per =  NULL) { //linked to table, database does not exist in the table okay, no 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 =  "[email protected]", Age = 12 }; Colls. Insertone (per); } //Modify  public static void updat () { //get data  var colls = db. Getcollection<persion> ("Persion");  var model = Colls. Find (c = c.name = = "Ynland"  ). FirstOrDefault ();//filter data  //conditions. Equivalent to where the filte.eq equals the 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 matches the field in the database, the mismatch cannot be found to automatically create a new field and assign a value  var update = Mongodb.driver.builders<persion>. Update.set ("Age",  "the");  // colls. Updateone (filter, update);//Modify a data  updateresult res= colls. Updatemany (filter, update);//Multiple Modification  } public static void deleteinfo () { var  Colls = db. Getcollection<persion> ("Persion"); Colls. Deleteone (c => c.age == 100); } //get Data 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

MONGODB official website Driver 2.2.4.26 version increase, deletion, check, mongodb2.2.4.26

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.