MongoDB 3.4版本, C# 驱动 2.4 操作
//数据库连接字符串 #region//获取表对象
IMongoCollection<Video> tb = db.GetCollection<Video>(CollectionName);
//先删除当前表
tb.Database.DropCollection(CollectionName);
//测试数据---------------------------------
var videos = new List<Video>
{
new Video {> Category="SciFi", Minutes=118 },
new Video {> Category="Horror", Minutes=122 },
new Video {> 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> tb.FindOneAndUpdate(x => x.Title == "The Perfect Developer",
Builders<Video>.Update.Set(x => x.Title, "A Perfect Developer ")
.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
页:
[1]