Project Update to MongoDB C # driver 2.2, found that the change from 1.9 to 2.0 is still very large, incorporating some of the commonly used operations to add demo code:
Class Program {const string collectionname = "video"; static void Main (string[] args) {//Remove the Demo collection then recreate later db. Getcollection<video> (CollectionName). Database.dropcollection (CollectionName); var videos = new List<video> {new Video {title= "the Perfect Developer", Category= "SciFi", minutes=118}, new Video {title= "Lost in Frankfurt am Main", Category= "Horror", minutes=122}, new Video {title= "the Infinite standup", Category= "Horror", minutes=341}}; Console.WriteLine ("Insert Videos ..."); Db. Getcollection<video> (CollectionName). Insertmany (VIDEOS); Console.WriteLine ("[After insert] ' all Videos:"); var all = db. Getcollection<video> (CollectionName). Find (X=>x.title! = string. EmptY). ToList (); foreach (Var v in) {Console.WriteLine (v); } Console.WriteLine ("Group by ..."); var groupby = db. Getcollection<video> (CollectionName). Aggregate (). Group (x = x.category, G = new {Name = G.key, Count = G.count (), totalminutes = g.sum (x = x.minutes)}) . ToList (); foreach (Var v in GroupBy) {Console.WriteLine (V.name + "," + V.count + "," + v.totalminutes "); } Console.WriteLine ("Updating One [Title = the Perfect Developer] ..."); Updating title with "The Perfect developer" video ' s ' title ' and ' minute ' db. Getcollection<video> (CollectionName). Findoneandupdate (X=>x.title = = "The Perfect Developer", Builders<video>. Update.set (x=> x.title, "A Perfect Developer [Updated]"). Addtoset (x = x.commenTS, "Good video!") . Addtoset (x = x.comments, "not bad")); Console.WriteLine ("[After Updating one] all Videos:"); all = db. Getcollection<video> (CollectionName). Find (x = x.title! = string. Empty). ToList (); foreach (Var v in) {Console.WriteLine (v); } Console.WriteLine ("Deleting One ... [Minutes = 122] "); Db. Getcollection<video> (CollectionName). Deleteone (x = x.minutes = = 122); Console.WriteLine ("[After Deleting one] all Videos:"); all = db. Getcollection<video> (CollectionName). Find (x = x.title! = string. Empty). ToList (); foreach (Var v in) {Console.WriteLine (v); } console.read (); } private static Imongodatabase db {get {var url = new Mongourl (confi Gurationsettings.appsettings["Mongourl "]); var client = new Mongoclient (URL); Return client. Getdatabase (URL. DatabaseName); }}} [bsonignoreextraelements] public class Video {public video () {Comments = new List<string> (); } [Bsonid] [Bsonrepresentation (Bsontype.objectid)] public string Id {get; set;} public string Title {get; set;} public string Category {get; set;} public int Minutes {get; set;} Public ilist<string> Comments {get; set;} public override string ToString () {return string. Format ("{0}-{1}-{2}", Title, Category, Minutes); } }
For MongoDB C # driver from 1.9 to 2.0, it simplifies the way the database is connected, simplifies the Find,update,delete interface, and the group by and projection operations are smoother.
In builders<t> the integration of update, filter, projection, and sort, the functionality of the cohesion is stronger, find function is very convenient.
The above is the content of MongoDB C # Driver Use Example (2.2), more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!