MONGODB official website Drive 2.2.4.26 version increase, deletion, check

Source: Internet
Author: User
Tags mongoclient

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 classpersion{//reference using MongoDB.Bson.Serialization.Attributes;[Bsonid] Public string_id;  Public stringName; Public intAge ;  Public stringEmail;  Public stringTableName {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 stringconn ="mongodb://127.0.0.1:27017"; //Database name        Static stringDatabase ="appdb";//Database name        StaticMongourl URL =NewMongourl (conn); StaticMongoclient ci =NewMongoclient (URL);//Create Mongoclient        StaticImongodatabase db = ci. Getdatabase (database);//Get Database

Here's how the data is done

//Add Data         Public Static voidInsertuser (persion per =NULL)        {            //link to table, the database does not exist in the table okay, there is no MongoDB will automatically create a collection//("Persion") collection also called Table            varColls = db. Getcollection<persion> ("persion"); Per=Newpersion {_id = Guid.NewGuid (). ToString (), Name ="[email protected]", age = A }; Colls.        Insertone (per); }        //Modify         Public  Static voidUpdat () {//Get Data            varColls = db. Getcollection<persion> ("persion"); varModel = 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)            varFilter = 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, and the mismatch cannot be found to automatically create a new field and assign a value            varUpdate = Mongodb.driver.builders<persion>. Update.set (" Age"," the"); //Colls. Updateone (filter, update);//Modify a piece of dataUpdateresult res= Colls. Updatemany (filter, update);//Multiple Modifications                             }         Public Static voidDeleteinfo () {varColls = db. Getcollection<persion> ("persion"); Colls. Deleteone (c= = C.age = = -); }        //Get Data         Public Static voidGetallinfo () {//data types must be consistent to not serialize            vart = 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

MONGODB official website Drive 2.2.4.26 version increase, deletion, check

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.