MongoDB
1.什麼是MongoDB?
ofollow,noindex">官網介紹 :MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need
維基百科 :MongoDB是一種面向文件的資料庫管理系統,由C++撰寫而成
百度百科 :MongoDB 是一個基於分散式文件 儲存的資料庫
2.為什麼我要使用MongoDB
最近寫一個專案,很多業務需要在排程中多執行緒處理。使用的是LINQ+EF,資料庫是msSQL,中間業務不算複雜,關係七八張表,中間有IO操作,GIT操作,CMD命令操作等 ..速度實在是沒有辦法忍受.
大概幾千個檔案提交需要執行30分鐘。。。。
後來瞭解到有MongoDB這種資料庫,效能高,靈活,擴充套件性高等等,再根據我們程式碼和業務的實際情況,就用準備測試一下實際情況
然後根據業務和一些效能考慮將排程程式碼分成三部分,
再次測速的幾個檔案提交只花了十幾秒鐘的時間。
MongoDB功不可沒。
3.下載
官網下載就可以了https://www.mongodb.com/
4.安裝
一直點下一步就好了
5.使用
貼一點程式碼,非真實專案中程式碼,還有需要修改的地方,比如反射沒使用委託等
using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using MongoDB.Driver; namespace Model { public static class MongoDBHelperNow<T> { /// <summary> /// 資料庫連線 /// </summary> static MongoClient client = new MongoClient("mongodb://192.168.20.94:27017");//ConstDefine.MongoDBConnectionString /// <summary> /// 資料庫名 /// </summary> private static readonly IMongoDatabase _gitDatabase = client.GetDatabase("Blog");//"Git"ConstDefine.MongoDBGitTableName public static IMongoDatabase GitDb { get { return _gitDatabase; } } public static IMongoCollection<T> GitTable(string keyname) => _gitDatabase.GetCollection<T>(keyname); private static string GetTableName() => typeof(T).ToString().Split('_')[1]; #region 增 public static void InsertOne(T entity) => GitTable(GetTableName()).InsertOne(entity); public static void InsertOneAsync(T entity) => GitTable(GetTableName()).InsertOneAsync(entity); public static void InsertList(List<T> entity) => GitTable(GetTableName()).InsertMany(entity); public static void InsertListAsync(List<T> entity) => GitTable(GetTableName()).InsertManyAsync(entity); #endregion #region 刪 public static void DeleteOne(Expression<Func<T, bool>> whereLambda) => GitTable(GetTableName()).FindOneAndDelete(whereLambda); public static void DeleteOneAsync(Expression<Func<T, bool>> whereLambda) => GitTable(GetTableName()).FindOneAndDeleteAsync(whereLambda); public static void DeleteList(Expression<Func<T, bool>> whereLambda) => GitTable(GetTableName()).DeleteMany(whereLambda); public static void DeleteListAsync(Expression<Func<T, bool>> whereLambda) => GitTable(GetTableName()).DeleteManyAsync(whereLambda); #endregion #region 查 public static T FindOne(Expression<Func<T, bool>> whereLambda) => GitTable(GetTableName()).Find(whereLambda).FirstOrDefault(); public static List<T> FindList(Expression<Func<T, bool>> whereLambda) => GitTable(GetTableName()).Find(whereLambda).ToList(); #endregion #region 改 public static T ReplaceOne(Expression<Func<T, bool>> whereLambda,T entity) => GitTable(GetTableName()).FindOneAndReplace(whereLambda, entity); public static Task<T> ReplaceOneAsync(Expression<Func<T, bool>> whereLambda, T entity) => GitTable(GetTableName()).FindOneAndReplaceAsync(whereLambda, entity); #endregion } }