MongoDB C# Driver 使用示例 (2.2)

黄舟
发布: 2017-02-28 11:48:32
原创
1541人浏览过

项目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)的内容,更多相关内容请关注PHP中文网(www.php.cn)!

芦笋演示
芦笋演示

一键出成片的录屏演示软件,专为制作产品演示、教学课程和使用教程而设计。

芦笋演示 34
查看详情 芦笋演示
相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号