码迷,mamicode.com
首页 > 数据库 > 详细

MongoDB 3.4版本, C# 驱动 2.4 操作

时间:2017-06-22 13:25:29      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:count   video   安装工具   client   count()   gre   img   window   evel   

 

 private static string _connStr = "mongodb://127.0.0.1:27017";
        private static string _dbName = "test";
        const string CollectionName = "sun";


 

  private static IMongoDatabase db
        {
            get
            {
                var url = new MongoUrl(_connStr);
                var client = new MongoClient(url);
                return client.GetDatabase(_dbName);
            }
        }


 


            //数据库连接字符串
            #region
            
            //获取表对象
            IMongoCollection<Video> tb = db.GetCollection<Video>(CollectionName);


            //先删除当前表
            tb.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 }
          };
            //测试数据---------------------------------

            //插入
            tb.InsertMany(videos);


            //查询
            var all = tb.Find(x => x.Title != string.Empty).ToList();

            //分组查询
            var groupby = tb.Aggregate()
                    .Group(x => x.Category, g => new { Name = g.Key, Count = g.Count(), TotalMinutes = g.Sum(x => x.Minutes) })
                    .ToList();
          

            //更新
            // updating title with "The perfect developer" video‘s ‘title‘ and ‘minute‘  
            tb.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")
                    );

            all = tb.Find(x => x.Title != string.Empty).ToList();
           


            //删除
            tb.DeleteOne(x => x.Minutes == 122);

            all = tb.Find(x => x.Title != string.Empty).ToList();
           

            #endregion

.



推荐使用2个MongoDB的 GUI 

1、MongoDBCompass

技术分享

2、RoboMongo

技术分享



windows下64位 MongoDB安装工具和GUI工具

http://pan.baidu.com/s/1c2gqJGO


.NET MongoDB 驱动

http://pan.baidu.com/s/1eRZ1eNo

MongoDB 3.4版本, C# 驱动 2.4 操作

标签:count   video   安装工具   client   count()   gre   img   window   evel   

原文地址:http://www.cnblogs.com/sunxueyang222/p/7064106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!