MongoDb C # Driver Operation example

Source: Internet
Author: User
Tags mongoclient

C # Operations MONGO database

Drive with Http://www.oschina.net/p/mongo-csharp-driver

C # Driven Basic database connection, adding or deleting operations//Defining objects
public class Person {public ObjectId _id;public string Name {get; set;} public int Age {get; set;} public override string ToString () {return string. Format ("id:{0} Name:{1} age:{2}", _id, Name, age);}}

  

  To add a reference:
Using mongodb.bson;using mongodb.driver;using mongodb.driver.builders;using MongoDB.Driver.Linq;

  

Connection Database String connectionstr = "Mongodb://localhost"; Mongoclient client = new Mongoclient (CONNECTIONSTR); Mongoserver Server = client. Getserver ();//Select database Mongodatabase db = server. Getdatabase ("person"); Select the Document collection Mongocollection<bsondocument> collection = db. GetCollection ("person");

  

Inserting Data *******************************************************************

Method 1:bsondocument person = new Bsondocument ();p Erson. ADD ("Name", "Test");p Erson. ADD ("Age", 10); Collection. Insert (person); Method 2:for (int i = 0; i <; i++) {var person = new Person () {Name = "Test" + i,age = i};collection. Insert (person);}

  

Querying Data *******************************************************************

Less than 20QueryDocument queryD1 = new Querydocument ("Age", New Querydocument ("$lt", 20)); foreach (var person in collection. Find (queryD1)) {Console.WriteLine (person);}
equals xq20querydocument queryD2 = new Querydocument ("Name", "test20"); foreach (var person in collection. Find (queryD2)) {Console.WriteLine (person);}
equals Xq20var Query1 = Query.and (Query.eq ("Name", "test20")); foreach (var person in collection. Find (Query1)) {Console.WriteLine (person);}

  

        

LINQ mode:

LINQ query var Query2 = collection. Asqueryable<person> (). Where (n = n.name.contains ("Test")). Take (20). ToList ();//. Where (n = = N.name = = "Xixihaha"). ToList (); foreach (var per in Query2) {Console.WriteLine (per);}

  

  

Save Data *******************************************************************

Save1 method var per = collection. Asqueryable<person> (). First (n = = N.name = = "Xixihaha");//Modify save data per. Age = 50;collection. Save (per);p er = collection. Asqueryable<person> (). First (n = n.name = = "Xixihaha"); Console.WriteLine (per);

Save2 method var query = Query.and (Query.eq ("Name", "Test5")); var document = collection. FindOne (query), if (document! = NULL) {document["age"] = 34;collection. Save (document);} var per = collection. Asqueryable<person> (). First (n = n.name = = "Test5"); Console.WriteLine (per);

The Update method var query = Query.and (Query.eq ("Name", "Test5")), var update = Update.set ("Age", and "collection"). Update (query, update); var per = collection. Asqueryable<person> (). First (n = n.name = = "Test5"); Console.WriteLine (per);

  

  

Delete Data *******************************************************************

Delete the specified document var query = Query.and (Query.eq ("Name", "Test5"));

  

Delete all document collection. RemoveAll ();

  

SOURCE Address: Link: Http://pan.baidu.com/s/1b2OGGY Password: CJP4

Reference Document: Http://www.cnblogs.com/wilber2013/p/4175825.html

MongoDb C # Driver Operation example

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.