Mongodb-getting Started with the C # Driver

Source: Internet
Author: User
Tags findone mongoclient

Summary: This article only provides a QuickStart level for using C # driver Operations MongoDB, a master skips

  1. Downloading the C # Driver
    1. Bash download
  2. To add a related DLL reference
        MongoDB.Bson.dll    MongoDB.Driver.dll
  3. Add a namespace reference
    using Mongodb.bson; using Mongodb.driver;
  4. Get Client Object
    var " Mongodb://localhost " ; var New Mongoclient (connectionString);
  5. Get server-side objects
    var server = client. Getserver ();
  6. Get the database to manipulate
    var database = server. Getdatabase ("test"//  "Test " is the name of the database
  7. CRUD (use a custom Class)
    1. Custom Entities
       Public class entity{    publicgetset;}      Public string Get Set ; }}
    2. Get the table to manipulate
      // "Entities" is the name of the collection var collection = database. Getcollection<entity> ("entities");
    3. Add a record
      var New " Tom " };collection. Insert (entity); var // Insert would set the Id if necessary (as it is in this example)
    4. Query a record
      var query = Query<entity>. EQ (E = e.id, Id); var entity = collection. FindOne (query);
    5. Save a record (send entire entity to database)
      " Dick " ; collection. Save (entity);
    6. Modify a record ( send only the modified part to the database, which is the same as the save or the difference, depending on the scene from the row to determine which one to use to update the data )
      var query = Query<entity>. EQ (E = e.id, Id); var " Harry " // Update Modifierscollection. Update (query, update);
    7. Delete a record
      var query = Query<entity>. EQ (E = e.id, Id); collection. Remove (query);

Full Demo Code:

1 usingMongodb.bson;2 usingMongodb.driver;3 usingMongoDB.Driver.Builders;4 usingSystem;5 usingSystem.Collections.Generic;6 usingSystem.Linq;7 usingSystem.Linq.Expressions;8 usingSystem.Text;9 usingSystem.Threading.Tasks;Ten  One namespacemongodbtest A { -     class Program -     { the         Static voidMain (string[] args) -         { -             varConnectionString ="mongodb://localhost:27017"; -             varClient =Newmongoclient (connectionString); +             varServer =client. Getserver (); -             varDatabase = Server. Getdatabase ("Test"); +             varCollection = database. Getcollection<entity> ("entities"); A             varentity =NewEntity {Name ="Tom" }; at             vari =collection. Insert (entity); -             varID =entity. Id; -  -             varquery = Query<entity>. EQ (E =e.id, Id); -entity =collection. FindOne (query); -Entity. Name ="Dick"; in             vars =collection. Save (entity); -  to             varUpdate = Update<entity>. Set (e = e.name,"Harry"); + collection. Update (query, update); -  the  * collection. Remove (query); $ Panax Notoginseng Console.readkey (); -  the         } +  A  the     } +      Public classEntity -     { $          PublicObjectId Id; $          Public stringName {Get;Set; } -     } -  the}

Mongodb-getting Started with the C # Driver

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.