1.什麼是MongoDB?git
官網介紹:MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you needmongodb
維基百科:MongoDB是一種面向文檔的數據庫管理系統,由C++撰寫而成數據庫
百度百科:MongoDB 是一個基於分佈式文件存儲的數據庫多線程
2.爲何我要使用MongoDB分佈式
最近寫一個項目,不少業務須要在調度中多線程處理。使用的是LINQ+EF,數據庫是msSQL,中間業務不算複雜,關係七八張表,中間有IO操做,GIT操做,CMD命令操做等 ..速度實在是沒有辦法忍受.性能
大概幾千個文件提交須要執行30分鐘。。。。 測試
後來瞭解到有MongoDB這種數據庫,性能高,靈活,擴展性高等等,再根據咱們代碼和業務的實際狀況,就用準備測試一下實際狀況flex
而後根據業務和一些性能考慮將調度代碼分紅三部分,url
再次測速的幾個文件提交只花了十幾秒鐘的時間。spa
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://127.0.0.1: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 } }