已經升級了mongodb至最新的3.4的版本,我想一想也該把驅動升到最新的了,我用的驅動仍是1.7的版本呢,以前幾回想升級,都是由於升級驅動須要改動的代碼太大了,升級的成本很高,因此懶得動,就這麼的用了幾年了。可是如今出了不少新功能,若是不升級的話,根本用不了,好比Lookup等功能。想一想咬牙就升級了。驅動DLL一換,就發現代碼全報錯了。之前的IMongoQuery等方法全用不了的。有一些方法也不見了,在修改的過程當中,只能寫邊修改邊寫代碼測試,一個一個測試它提供的新方法。等了解了新的驅動後,以爲改動仍是很是大的,將Query,Update等都集成到了一個叫Builders的類中了,把Update中有關SetWrapped的方法全刪了,最後試了半天,才發現,SetWrapped的方法,能夠直接用新方法中的Set就能夠搞定了,而AddToSetWrapped方法只須要用AddToSet方法就好了。程序員
帶給個人最大震憾是,新的寫法能夠徹底用LINQ搞定,終於不用再去管mongodb的那個實體類的映射了,在操做中,徹底用類的字段就能夠搞定,我把的一個類的方法貼出來供你們參考一下。mongodb
public class Content { [BsonId] public ObjectId Id { get; set; } [BsonElement("t")] public string Title { get; set; } [BsonElement("d")] public string Detail { get; set; } [BsonElement("a")] public string Author { get; set; } [BsonElement("ct")] public double CreateTime { get; set; } [BsonElement("click")] public int Click { get; set; } [BsonElement("img")] public string Img { get; set; } [BsonElement("f")] public bool IsFinished { get; set; } [BsonElement("type")] public int Type { get; set; } }
老的操做類的土鱉方法是這樣的數據庫
public class ContentInfo : DBBase { public static void InsertContent(string title, string detail, string author, string img, int type) { Content k = new Content { Author = author, Detail = detail, Img = img, Title = title, Click = 0, Type = type, CreateTime = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000) }; try { var collection = _database.GetCollection<Content>("content"); collection.Insert(k); } catch (Exception ex) { Console.WriteLine(ex.Message); } } public static void UpdateContent(string id, string title, string detail, string author, string img, int type) { var collection = _database.GetCollection<Content>("content"); IMongoQuery query = Query.EQ("_id", new ObjectId(id)); UpdateBuilder up = new UpdateBuilder(); up.Set("t", title); up.Set("d", detail); up.Set("a", author); up.Set("img", img); up.Set("type", type); collection.Update(query, up, UpdateFlags.Upsert); } public static Content Get(string id) { var collection = _database.GetCollection<Content>("content"); IMongoQuery query = Query.EQ("_id", new ObjectId(id)); IMongoUpdate update = Update.Inc("click", 1); collection.Update(query, update); var results = collection.FindOneAs<Content>(query); return results; } public static List<Content> GetContentList(int pageIndex, int pageSize, out int count, bool isShow = true) { var collection = _database.GetCollection<Content>("content"); IMongoQuery query = Query.GT("click", -100); if (isShow) { query = Query.And(query, Query.EQ("f", true)); } SortByDocument sort = new SortByDocument { { "ct", -1 } }; var num = collection.Count(query); count = Convert.ToInt32(num); var results = collection.FindAs<Content>(query).SetSortOrder(sort).SetSkip((pageIndex - 1) * pageSize).SetLimit(pageSize); return results.ToList(); } public static List<Content> GetContentTypeList(int type, int pageIndex, int pageSize, out int count) { var collection = _database.GetCollection<Content>("content"); IMongoQuery query = Query.GT("click", -100); query = Query.And(query, Query.EQ("f", true)); query = Query.And(query, Query.EQ("type", type)); SortByDocument sort = new SortByDocument { { "ct", -1 } }; var num = collection.Count(query); count = Convert.ToInt32(num); var results = collection.FindAs<Content>(query).SetSortOrder(sort).SetSkip((pageIndex - 1) * pageSize).SetLimit(pageSize); return results.ToList(); } public static List<Content> GetContentSiteMap() { var collection = _database.GetCollection<Content>("content"); IMongoQuery query = Query.GT("click", -100); query = Query.And(query, Query.EQ("f", true)); SortByDocument sort = new SortByDocument { { "ct", -1 } }; FieldsDocument fd = new FieldsDocument(); fd.Add("d", 0); var results = collection.FindAs<Content>(query).SetFields(fd).SetSortOrder(sort); return results.ToList(); } public static void UpdateContentTrue(string id) { var collection = _database.GetCollection<Content>("content"); IMongoQuery query = Query.EQ("_id", new ObjectId(id)); UpdateBuilder up = new UpdateBuilder(); up.Set("f", true); up.Set("ct", ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000)); collection.Update(query, up, UpdateFlags.Upsert); }
}
而新的方法是這樣的:app
public class ContentInfo : DBBase { public static void InsertContent(string title, string detail, string author, string img, int type) { Content k = new Content { Author = author, Detail = detail, Img = img, Title = title, Click = 0, Type = type, CreateTime = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000) }; try { var collection = _database.GetCollection<Content>("content"); collection.InsertOne(k); } catch (Exception ex) { Console.WriteLine(ex.Message); } } public static void UpdateContent(string id, string title, string detail, string author, string img, int type) { var collection = _database.GetCollection<Content>("content"); var filter = Builders<Content>.Filter.Eq(x => x.Id, new ObjectId(id)); var up = Builders<Content>.Update .Set(x => x.Title, title) .Set(x => x.Detail, detail) .Set(x => x.Author, author) .Set(x => x.Img, img) .Set(x => x.Type, type); collection.UpdateOneAsync(filter, up); } public static Content Get(string id) { var collection = _database.GetCollection<Content>("content"); var filter = Builders<Content>.Filter.Eq(x => x.Id, new ObjectId(id)); var up = Builders<Content>.Update.Inc(x => x.Click, 1); var results = collection.FindOneAndUpdate(filter, up); return results; } public static List<Content> GetContentList(int pageIndex, int pageSize, out int count, bool isShow = true) { var collection = _database.GetCollection<Content>("content"); var builder = Builders<Content>.Filter; var filter = builder.Gt(x => x.Click, -100); if (isShow) { filter = builder.And(filter, builder.Eq(x => x.IsFinished, true)); } var sort = Builders<Content>.Sort.Descending(x => x.CreateTime); var num = collection.Count(filter); count = Convert.ToInt32(num); var results = collection.Find(filter).Sort(sort).Skip((pageIndex - 1) * pageSize).Limit(pageSize); return results.ToList(); } public static List<Content> GetContentTypeList(int type, int pageIndex, int pageSize, out int count) { var collection = _database.GetCollection<Content>("content"); var filter = Builders<Content>.Filter.Where(x => x.Click > -100 && x.IsFinished == true && x.Type == type); var sort = Builders<Content>.Sort.Descending(x => x.CreateTime); var num = collection.Count(filter); count = Convert.ToInt32(num); var results = collection.Find(filter).Sort(sort).Skip((pageIndex - 1) * pageSize).Limit(pageSize); return results.ToList(); } public static List<Content> GetContentSiteMap() { var collection = _database.GetCollection<Content>("content"); var filter = Builders<Content>.Filter.Where(x => x.Click > -100 && x.IsFinished == true); var sort = Builders<Content>.Sort.Descending(x => x.CreateTime); var project = Builders<Content>.Projection.Exclude(x => x.Detail); var results = collection.Find(filter).Project<Content>(project).Sort(sort); return results.ToList(); } public static void UpdateContentTrue(string id) { var collection = _database.GetCollection<Content>("content"); var filter = Builders<Content>.Filter.Eq(x => x.Id, new ObjectId(id)); var up = Builders<Content>.Update .Set(x => x.IsFinished, true) .Set(x => x.CreateTime, ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000)); var results = collection.FindOneAndUpdate(filter, up); } }
新的裏面已經徹底看不到了mongodb數據庫中的字段名了,徹底能夠用lambda表達式完成這些操做了,對程序員來講,操做簡便了,不須要再去對照映射關係了,也下降了一些程序員的入門門檻,有些基礎的操做,程序員甚至不須要了解mongodb,只要會寫LINQ就能夠完成了。這麼多的好處,我以爲將數據庫和驅動都升級到最新版仍是挺值的。可是注意,1.X版本的驅動和2.X版本的驅動是徹底不同的,是破壞性升級,若是想要更換這個驅動,代碼是必定要從新寫的,因此你們也別光看到好處,也要計算一下成本。測試