MongoDB 3.4版本, C# 驅動 2.4 操作
阿新 • • 發佈:2018-12-24
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