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 } }