MongoDB C# Driver 使用樣本 (2.2)

來源:互聯網
上載者:User
項目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'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 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操作也更流暢了。
在Builders<T>中整合了 update, filter, projection, 和sort,功能的內聚性更強了,找function很方便。

以上就是MongoDB C# Driver 使用樣本 (2.2)的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.