. NET using MongoDB first experience

Source: Internet
Author: User
Tags install mongodb mongoclient mongodb driver


0) Basic Preparation

Install MongoDB driver (current article is based on MongoDB.Driver.dll, v1.7.0.4714)

Using mongodb.bson;using mongodb.driver;


1) Create a connection

#0) Prepare the connection string and dbprivate static readonly string _connectionstring = "Mongodb://localhost";p rivate static readonly string _ DbName = "Test";//1) Create Connectionvar _mongoclient = new Mongoclient (_connectionstring); var _server = _ Mongoclient.getserver (); var _db = _server. Getdatabase (_dbname); var collection = _db. GetCollection ("Redpack");

2) Implementation of additions and deletions

dictionary<string, string> contact = new Dictionary<string, string>;contact. ADD ("Mch_billno", "12345678"); ADD ("Pername", "King Mo"); contact. ADD ("Sex", "male"); ADD ("Age", "35"),//2) Insert Bsondocument doc = new bsondocument ();d OC. Setdocumentid (contact["Mch_billno"]);//In order to achieve their own maintenance doc. ADD (Contact); collection. Insert (DOC);//3) update var query = new Querydocument {{"_id", contact["Mch_billno"]}};var Updatebson = new Bsondocument (); u Pdatebson.add (Contact), var updatedocument = new UpdateDocument {"$set", Updatebson}};collection. Update (query, updatedocument);



. NET using MongoDB first experience

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.