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