MongoDB

1.什么是MongoDB?

官网介绍: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://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
	}
}   
posted @ 2018-10-31 19:29  HANFAN  阅读(...)  评论(...编辑  收藏