首頁  >  文章  >  後端開發  >  MongoDB C# Driver 使用範例 (2.2)

MongoDB C# Driver 使用範例 (2.2)

黄舟
黄舟原創
2017-02-28 11:48:321429瀏覽

專案update到了mongoDB C# driver 2.2 , 發現從1.9到2.0的變化還是很大的,整合了一些常用的操作附加demo程式碼:

  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 all)
            {
                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&#39;s &#39;title&#39; and &#39;minute&#39;
            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 all)
            {
                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 all)
            {
                Console.WriteLine(v);
            }


            Console.Read();
        }


        private static IMongoDatabase db
        {
            get
            {
                var url = new MongoUrl(ConfigurationSettings.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);
        }
    }

對於mongoDB C# driver從1.9到2.0的更新,簡化了資料庫的連接方式,簡化了Find,Update,Delete的接口,group by和projection操作也更流暢了。
在Builders8742468051c85b06f0a0af9e3e506b5c中整合了 update, filter, projection, 和sort,功能的內聚性更強了,找function很方便。
 

 以上是MongoDB C# Driver 使用範例 (2.2)的內容,更多相關內容請關注PHP中文網(www.php.cn)!


#
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn